4

我正在尝试将反射库包含在示例 jar 中,但无法使其正常工作:

$ kotlinc hello.kt -d hello.jar
$ java -jar hello.jar
Exception in thread "main" java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics

缺少运行时库,所以让我们添加它:

$ kotlinc hello.kt -include-runtime -d hello.jar
$ java -jar hello.jar
Exception in thread "main" kotlin.jvm.KotlinReflectionNotSupportedError: Kotlin reflection implementation is not found at runtime. Make sure you have kotlin-reflect.jar in the classpath

现在包含了运行时库,但是缺少反射库,所以让我们指定 kotlin 主目录:

$ kotlinc hello.kt -include-runtime -kotlin-home ~/.sdkman/candidates/kotlin/1.1.1 -d hello.jar
$ java -jar hello.jar
Exception in thread "main" kotlin.jvm.KotlinReflectionNotSupportedError: Kotlin reflection implementation is not found at runtime. Make sure you have kotlin-reflect.jar in the classpath

反射库仍然不包括在内。kotlinchelp 列出了一个 ' -no-reflect' 选项,所以我假设在-include-runtime设置 ' ' 时默认情况下应该包含反射库,但情况似乎并非如此。

4

1 回答 1

6

目前,该-include-runtime选项仅将标准 Kotlin 库 ( kotlin-stdlib) 包含到生成的 jar 中。你说得对,反射库 ( kotlin-reflect) 也应该包括在内,除非-no-reflect指定了选项。我在跟踪器中创建了一个问题:https ://youtrack.jetbrains.com/issue/KT-17344

在问题解决之前:如果您正在寻找一种仅在您的机器上运行已编译程序的方法,我建议您将应用程序编译到目录而不是 jar,然后使用kotlin命令运行主类。

kotlinc hello.kt -d output
kotlin -cp output hello.HelloKt

但是,如果您打算将程序分发给其他用户,我建议使用适当的构建系统,如 Maven 或 Gradle,并在那里指定kotlin-stdlib/kotlin-reflect作为适当的依赖项,而不是将它们与您的应用程序捆绑在单个 jar 中。

于 2017-04-11T09:20:54.077 回答