在JAR中打包Kotlin .kt类

时间:2017-10-27 16:30:42

标签: jar jvm package kotlin manifest

如何将HelloWorld.kt构建为JAR以便它运行?

thufir@dur:~/kotlin$ 
thufir@dur:~/kotlin$ kotlinc HelloWorld.kt --include-runtime -d HelloWorld.jar
error: invalid argument: --include-runtime
info: use -help for more information
thufir@dur:~/kotlin$ 
thufir@dur:~/kotlin$ 
thufir@dur:~/kotlin$ kotlinc HelloWorld.kt -d HelloWorld.jar
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.intellij.util.text.StringFactory to constructor java.lang.String(char[],boolean)
WARNING: Please consider reporting this to the maintainers of com.intellij.util.text.StringFactory
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
thufir@dur:~/kotlin$ 
thufir@dur:~/kotlin$ java -jar HelloWorld.jar 
no main manifest attribute, in HelloWorld.jar
thufir@dur:~/kotlin$ 
thufir@dur:~/kotlin$ cat HelloWorld.kt 
import kotlin.jvm.JvmStatic

object HelloWorld {
    @JvmStatic
    public fun main(args: Array<String>) {
        println("Hello, world!" + args[0])
    }
}
thufir@dur:~/kotlin$ 

请注意,它已声明为类,因此该方法不在顶层(?)。还希望包含kotlin运行时。

我是否需要为entry point手动创建MANIFEST.MF Main-Class

1 个答案:

答案 0 :(得分:1)

您应该使用像Gradle这样的构建工具来执行该任务。

在此示例中,正在创建一个带有清单条目的Jar:https://github.com/s1monw1/kotlin_vertx_example/blob/master/build.gradle.kts