在由kotlinc输出的jar中包含反射库

我试图让包含在示例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 '被设置-include-runtime ,默认情况下应该包含反射库,但是这似乎并不是这样。

目前, -include-runtime选项只包含标准的Kotlin库( kotlin-stdlib )到生成的jar文件。 除非指定了-no-reflect选项,否则也应该包含反射库( kotlin-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作为适当的依赖关系,而不是将它们与应用程序捆绑在一起单个罐子。