package Kotlin .kt class in a JAR

2019-07-19 04:53发布

问题:

How do I build HelloWorld.kt as a JAR so that it runs?

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$ 

Note that it's declared as a class, and so the method is not on the top level(?). Would want to include the kotlin runtime as well.

Do I need to manually create a MANIFEST.MF with Main-Class for the entry point?

回答1:

You should use a build tool like Gradle for that task.

In this example, a Jar with Manifest entry is being created: https://github.com/s1monw1/kotlin_vertx_example/blob/master/build.gradle.kts