我正在尝试获取示例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反射库仍未包括在内。kotlinc帮助列出了一个'-no-reflect‘选项,所以我假设在设置'-include-runtime’时,默认情况下应该包括反射库,但情况似乎并非如此。
发布于 2017-04-11 09:20:54
目前,-include-runtime选项仅包括对结果jar的标准Kotlin库(kotlin-stdlib)。您也应该包括反射库(kotlin-reflect),除非指定了-no-reflect选项。我在跟踪器中创建了一个问题:https://youtrack.jetbrains.com/issue/KT-17344
在问题解决之前:如果您只想在机器上运行编译过的程序,我建议您将应用程序编译到目录而不是jar,然后使用kotlin命令运行main类。
kotlinc hello.kt -d output
kotlin -cp output hello.HelloKt如果您计划将该程序分发给其他用户,我建议使用Maven或Gradle这样的构建系统,并将kotlin-stdlib/kotlin-reflect指定为适当的依赖项,而不是将它们与您的应用程序捆绑在一起。
https://stackoverflow.com/questions/43337513
复制相似问题