jar - 在JAR中将Kotlin .kt类打包

标签 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.MFMain-Class吗?

最佳答案

您应该为该任务使用诸如Gradle之类的构建工具。

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

关于jar - 在JAR中将Kotlin .kt类打包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46979678/

相关文章:

java - 作为 jar 运行时出现 FileNotFoundException

android - 任务 ':app:dexDebug' 执行失败

java - 将项目 (jar) 导入 Eclipse

java - Minor GC 和 Full GC 同时进行?

reactjs - 如何将依赖于样式组件的 React 组件库提供给另一个也具有样式组件依赖项的库?

R - 无法访问包安装的 url

eclipse - 如何在gradle文件夹中下载jar文件和pom文件?

garbage-collection - xmx 和 gc log max heap size 的区别?

jvm - 增加堆大小后无法启动Glassfish

node.js - npm 安装 zmq 失败