HoRain云--Kotlin命令行编译终极指南:从入门到精通
HoRain 云小助手个人主页⛺️生活的理想就是为了理想的生活!⛳️ 推荐前些天发现了一个超棒的服务器购买网站性价比超高大内存超划算忍不住分享一下给大家。点击跳转到网站。目录⛳️ 推荐Kotlin 命令行编译完整指南一、环境准备1. 安装 Kotlin 编译器2. 验证安装二、基本编译和运行1. 创建一个简单的 Kotlin 文件2. 编译为 JAR 文件3. 运行程序三、编译选项详解1. 输出相关选项2. 包含运行时3. 类路径和依赖4. 编译器选项四、多文件编译1. 编译多个文件2. 示例项目结构五、使用 KTS 脚本1. 创建 Kotlin 脚本文件2. 运行脚本3. 脚本编译选项六、使用 kts 编译器工具1. kotlinc-js编译到 JavaScript2. kotlinc-native编译到原生七、高级编译配置1. 使用模块文件2. 生成文档3. 增量编译八、编译示例项目1. 简单多文件项目2. 使用外部库的项目九、使用 Gradle 构建替代方案1. 创建简单的 build.gradle.kts2. 构建和运行十、故障排除1. 常见错误及解决方案2. 调试编译十一、优化技巧1. 使用编译缓存2. 并行编译3. 优化输出大小十二、实用脚本1. 自动编译和运行脚本2. 监视并自动编译总结Kotlin 命令行编译完整指南Kotlin 提供了多种命令行编译方式让你可以在不依赖 IDE 的情况下编译和运行 Kotlin 程序。一、环境准备1. 安装 Kotlin 编译器方法一通过 SDKMAN!Linux/macOS# 安装 SDKMAN curl -s https://get.sdkman.io | bash source $HOME/.sdkman/bin/sdkman-init.sh # 安装 Kotlin sdk install kotlin方法二通过 HomebrewmacOSbrew update brew install kotlin方法三手动安装访问 Kotlin 发布页面下载 kotlin-compiler-*.zip解压并添加到 PATH在.bashrc或.bash_profile中添加export PATH/path/to/kotlinc/bin:$PATH方法四通过包管理器Windows# 使用 Chocolatey choco install kotlin # 或使用 Scoop scoop install kotlin方法五通过包管理器Linux# Ubuntu/Debian sudo snap install --classic kotlin # 或从下载页面下载 tar.gz2. 验证安装kotlinc -version # 应显示类似info: kotlinc-jvm 1.9.0 (JRE 11.0.19)二、基本编译和运行1. 创建一个简单的 Kotlin 文件创建hello.ktfun main() { println(Hello, Kotlin!) }2. 编译为 JAR 文件# 基本编译包含运行时 kotlinc hello.kt -include-runtime -d hello.jar # 编译不包含运行时需要 Kotlin 运行时库 kotlinc hello.kt -d hello.jar3. 运行程序# 如果编译时包含了运行时 java -jar hello.jar # 如果编译时不包含运行时 kotlin hello.jar # 或 java -cp hello.jar:$KOTLIN_HOME/lib/kotlin-stdlib.jar HelloKt三、编译选项详解1. 输出相关选项# 指定输出文件/目录 kotlinc hello.kt -d output.jar kotlinc hello.kt -d output/ # 指定输出格式 kotlinc hello.kt -d output.jar # JAR 文件 kotlinc hello.kt -d output/ # 目录2. 包含运行时# 包含标准库和运行时 kotlinc hello.kt -include-runtime -d app.jar # 指定运行时版本 kotlinc hello.kt -kotlin-home /path/to/kotlin -d app.jar3. 类路径和依赖# 添加类路径 kotlinc hello.kt -cp libs/* -d app.jar # 添加多个依赖 kotlinc hello.kt -cp lib1.jar:lib2.jar:/path/to/lib -d app.jar4. 编译器选项# 启用详细输出 kotlinc hello.kt -verbose -d app.jar # 指定 Java 版本 kotlinc hello.kt -jvm-target 11 -d app.jar # 启用警告 kotlinc hello.kt -Werror -d app.jar kotlinc hello.kt -nowarn -d app.jar # 禁用警告 # 指定语言版本 kotlinc hello.kt -language-version 1.9 -d app.jar kotlinc hello.kt -api-version 1.9 -d app.jar四、多文件编译1. 编译多个文件# 列出所有文件 kotlinc file1.kt file2.kt file3.kt -include-runtime -d app.jar # 使用通配符 kotlinc src/*.kt -include-runtime -d app.jar # 递归编译 kotlinc src/**/*.kt -include-runtime -d app.jar2. 示例项目结构myapp/ ├── src/ │ ├── main.kt │ ├── utils/ │ │ └── StringUtils.kt │ └── models/ │ └── User.kt └── lib/ └── dependency.jar编译命令kotlinc src/*.kt src/utils/*.kt src/models/*.kt -cp lib/dependency.jar -include-runtime -d myapp.jar五、使用 KTS 脚本1. 创建 Kotlin 脚本文件创建script.kts#!/usr/bin/env kotlin file:DependsOn(com.google.code.gson:gson:2.8.9) import com.google.gson.Gson val gson Gson() println(gson.toJson(mapOf(message to Hello from Kotlin Script!)))2. 运行脚本# 直接运行 kotlin script.kts # 或作为可执行文件 chmod x script.kts ./script.kts3. 脚本编译选项# 预编译脚本以提高性能 kotlinc -script script.kts -main com.example.MyScript -d script.jar # 运行预编译的脚本 kotlin script.jar六、使用 kts 编译器工具1. kotlinc-js编译到 JavaScript# 安装 Kotlin/JS # 通常与 kotlinc 一起安装 # 编译到 JavaScript kotlinc-js hello.kt -output hello.js # 运行 node hello.js2. kotlinc-native编译到原生# 安装 Kotlin/Native # 下载并解压到独立目录 # 编译到可执行文件 kotlinc-native hello.kt -o hello # 运行 ./hello.kexe七、高级编译配置1. 使用模块文件创建module.xmlmodule namemyapp version1.0 sources dirsrc/main/kotlin/dir /sources dependencies jarlib/dependency.jar/jar /dependencies /module编译kotlinc -module-xml module.xml -d myapp.jar2. 生成文档# 生成 KDocs kotlinc hello.kt -d hello.jar -include-runtime -dokka # 指定文档格式 kotlinc hello.kt -d hello.jar -include-runtime -dokka -format html3. 增量编译kotlinc hello.kt -d output/ -Xincremental八、编译示例项目1. 简单多文件项目目录结构calc/ ├── src/ │ ├── main.kt │ └── math/ │ ├── Calculator.kt │ └── Operations.kt └── build.shsrc/main.kt:import math.Calculator fun main() { val calc Calculator() println(5 3 ${calc.add(5, 3)}) println(10 - 4 ${calc.subtract(10, 4)}) }src/math/Calculator.kt:package math class Calculator { fun add(a: Int, b: Int): Int a b fun subtract(a: Int, b: Int): Int a - b fun multiply(a: Int, b: Int): Int a * b fun divide(a: Int, b: Int): Int a / b }编译脚本 build.sh:#!/bin/bash # 创建输出目录 mkdir -p out # 编译所有 Kotlin 文件 kotlinc src/*.kt src/math/*.kt -include-runtime -d out/calc.jar echo 编译完成运行: java -jar out/calc.jar2. 使用外部库的项目build.sh:#!/bin/bash # 创建 lib 目录并下载依赖 mkdir -p lib cd lib # 下载依赖示例Gson if [ ! -f gson-2.8.9.jar ]; then wget https://repo1.maven.org/maven2/com/google/code/gson/gson/2.8.9/gson-2.8.9.jar fi cd .. # 编译 kotlinc src/*.kt -cp lib/* -include-runtime -d app.jar echo 编译完成运行: java -jar app.jar九、使用 Gradle 构建替代方案虽然命令行编译简单但对于复杂项目建议使用构建工具1. 创建简单的 build.gradle.ktsplugins { kotlin(jvm) version 1.9.0 } group com.example version 1.0 repositories { mavenCentral() } dependencies { implementation(kotlin(stdlib)) } tasks.jar { manifest { attributes[Main-Class] com.example.MainKt } from(configurations.runtimeClasspath.get().map { if (it.isDirectory) it else zipTree(it) }) }2. 构建和运行# 构建 ./gradlew build # 运行 java -jar build/libs/myapp-1.0.jar十、故障排除1. 常见错误及解决方案错误未找到 kotlinc 命令# 检查 PATH echo $PATH # 手动指定路径 /opt/kotlinc/bin/kotlinc hello.kt -d hello.jar错误找不到主类# 确保 main 函数存在 # 检查 MANIFEST.MF jar tf hello.jar | grep META-INF # 或指定主类运行 kotlin -classpath hello.jar HelloKt错误不支持的类文件版本# 指定 JVM 目标版本 kotlinc hello.kt -jvm-target 1.8 -d hello.jar # 检查 Java 版本 java -version错误内存不足# 增加堆内存 kotlinc -J-Xmx2g hello.kt -d hello.jar2. 调试编译# 显示详细输出 kotlinc -verbose hello.kt -d hello.jar # 显示编译时间 kotlinc -Xreport-perf hello.kt -d hello.jar # 生成 IR 树 kotlinc hello.kt -Xdump-declarations-todeclarations.txt十一、优化技巧1. 使用编译缓存# 启用编译缓存 kotlinc hello.kt -d out/ -Xuse-fir-lm # 清理缓存 rm -rf ~/.kotlin/2. 并行编译# 启用并行编译 kotlinc hello.kt -Xmulti-platform -d hello.jar3. 优化输出大小# 启用 ProGuard/R8 kotlinc hello.kt -include-runtime -d hello.jar -Xuse-r8十二、实用脚本1. 自动编译和运行脚本build-and-run.sh:#!/bin/bash # 配置 SOURCE_DIRsrc OUTPUT_JARapp.jar MAIN_CLASSMainKt # 清理 rm -f $OUTPUT_JAR # 编译 echo 正在编译... kotlinc $SOURCE_DIR/*.kt -include-runtime -d $OUTPUT_JAR if [ $? -eq 0 ]; then echo 编译成功 echo 运行程序... echo java -jar $OUTPUT_JAR else echo 编译失败 exit 1 fi2. 监视并自动编译watch-and-build.sh:#!/bin/bash # 监视文件变化并自动编译 watch_dirsrc output_jarapp.jar echo 正在监视 $watch_dir 目录中的 Kotlin 文件... # 使用 inotifywait (Linux) 或 fswatch (macOS) # Linux: inotifywait -m -r -e modify,create,delete $watch_dir | while read -r directory events filename; do if [[ $filename *.kt ]]; then echo 检测到变化: $filename kotlinc $watch_dir/*.kt -include-runtime -d $output_jar if [ $? -eq 0 ]; then echo $(date): 编译成功 fi fi done总结Kotlin 命令行编译提供了灵活的选项基本编译kotlinc file.kt -include-runtime -d output.jar运行程序java -jar output.jar或kotlin output.jar多文件编译列出所有文件或使用通配符添加依赖使用-cp参数指定类路径脚本支持使用.kts文件编写可执行脚本对于简单项目命令行编译足够使用。对于复杂项目建议使用构建工具如 Gradle 或 Maven。命令行编译是学习 Kotlin 和快速原型开发的好方法也适合 CI/CD 环境中的自动化构建。❤️❤️❤️本人水平有限如有纰漏欢迎各位大佬评论批评指正如果觉得这篇文对你有帮助的话也请给个点赞、收藏下吧非常感谢! Stay Hungry Stay Foolish 道阻且长,行则将至,让我们一起加油吧