Kotlin:如何创建一个可运行的jar?
我正试图用Kotlin创建一个可运行的jar 我的gradle.build是这样的:Kotlin:如何创建一个可运行的jar?,kotlin,jar,Kotlin,Jar,我正试图用Kotlin创建一个可运行的jar 我的gradle.build是这样的: plugins { id 'org.jetbrains.kotlin.jvm' version '1.3.11' } group 'com.github.dynamik' version '1.0-SNAPSHOT' apply plugin: 'application' apply plugin: 'kotlin' mainClassName = "interpreter.Repl"
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.3.11'
}
group 'com.github.dynamik'
version '1.0-SNAPSHOT'
apply plugin: 'application'
apply plugin: 'kotlin'
mainClassName = "interpreter.Repl"
repositories {
mavenCentral()
maven { setUrl("https://dl.bintray.com/hotkeytlt/maven") }
}
configurations {
ktlint
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
compile 'com.github.h0tk3y.betterParse:better-parse-jvm:0.4.0-alpha-3'
// https://mvnrepository.com/artifact/junit/junit
testCompile group: 'junit', name: 'junit', version: '4.4'
ktlint "com.github.shyiko:ktlint:0.31.0"
implementation 'com.github.ajalt:clikt:1.7.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.2.0'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
run {
standardInput = System.in
}
jar {
manifest {
attributes 'Main-Class': 'interpreter.Repl'
}
}
(目前,当我运行/gradlew
时,一切正常。)
我正在读一篇关于如何继续的文章,它说要做:java-jar.jar
我不太明白--我们在哪里运行这个?我尝试从项目根目录运行它,但出现错误:
Error: Unable to access jarfile <my_jarname>.jar
错误:无法访问jarfile.jar
使用Kotlin主类时,需要在类名称末尾添加一个Kt
,同时在清单上引用它。
因此,如果您的主类名为解释器.Repl
,请使用:
jar {
manifest {
attributes 'Main-Class': 'interpreter.ReplKt'
}
}
而不是
jar {
manifest {
attributes 'Main-Class': 'interpreter.Repl'
}
}
好吧,我想出来了:)
因此,创建jar的方法是:/gradlew build
。这将在build/libs
中创建一个jar
问题是,在运行该jar时,会遇到一个关于java.lang.intrinsics
的异常,因为kotlin stdlib
尚未打包到jar中
虽然有一种手动实现的方法,但我发现最简单的解决方案是简单地使用shadowjar插件
我的build.gradle最终看起来像这样:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.4'
}
}
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.3.11'
}
group 'com.github.dynamik'
version '1.0-SNAPSHOT'
apply plugin: 'application'
apply plugin: 'kotlin'
apply plugin: 'java'
mainClassName = "interpreter.Repl"
repositories {
mavenCentral()
jcenter()
maven { setUrl("https://dl.bintray.com/hotkeytlt/maven") }
maven {
url = uri("https://plugins.gradle.org/m2/")
}
}
configurations {
ktlint
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
compile 'com.github.h0tk3y.betterParse:better-parse-jvm:0.4.0-alpha-3'
// https://mvnrepository.com/artifact/junit/junit
testCompile group: 'junit', name: 'junit', version: '4.4'
ktlint "com.github.shyiko:ktlint:0.31.0"
implementation 'com.github.ajalt:clikt:1.7.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.2.0'
}
apply plugin: 'com.github.johnrengelman.shadow'
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
run {
standardInput = System.in
}
jar {
manifest {
attributes 'Main-Class': 'interpreter.Repl'
}
}
tasks.register<Jar>("uberJar") {
archiveClassifier.set("uber")
manifest {
attributes(
"Main-Class" to "mytest.AppKt",
"Implementation-Title" to "Gradle",
"Implementation-Version" to archiveVersion
)
}
from(sourceSets.main.get().output)
dependsOn(configurations.runtimeClasspath)
from({
configurations.runtimeClasspath.get().filter { it.name.endsWith("jar") }.map { zipTree(it) }
})
}
从Gradle 5.4.1开始,一个
build.Gradle.kts
需要这样一个部分:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.4'
}
}
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.3.11'
}
group 'com.github.dynamik'
version '1.0-SNAPSHOT'
apply plugin: 'application'
apply plugin: 'kotlin'
apply plugin: 'java'
mainClassName = "interpreter.Repl"
repositories {
mavenCentral()
jcenter()
maven { setUrl("https://dl.bintray.com/hotkeytlt/maven") }
maven {
url = uri("https://plugins.gradle.org/m2/")
}
}
configurations {
ktlint
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
compile 'com.github.h0tk3y.betterParse:better-parse-jvm:0.4.0-alpha-3'
// https://mvnrepository.com/artifact/junit/junit
testCompile group: 'junit', name: 'junit', version: '4.4'
ktlint "com.github.shyiko:ktlint:0.31.0"
implementation 'com.github.ajalt:clikt:1.7.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.2.0'
}
apply plugin: 'com.github.johnrengelman.shadow'
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
run {
standardInput = System.in
}
jar {
manifest {
attributes 'Main-Class': 'interpreter.Repl'
}
}
tasks.register<Jar>("uberJar") {
archiveClassifier.set("uber")
manifest {
attributes(
"Main-Class" to "mytest.AppKt",
"Implementation-Title" to "Gradle",
"Implementation-Version" to archiveVersion
)
}
from(sourceSets.main.get().output)
dependsOn(configurations.runtimeClasspath)
from({
configurations.runtimeClasspath.get().filter { it.name.endsWith("jar") }.map { zipTree(it) }
})
}
tasks.register(“uberJar”){
archiveClassifier.set(“uber”)
显示{
属性(
“主类”到“mytest.AppKt”,
“实施标题”改为“Gradle”,
“实施版本”到archiveVersion
)
}
from(sourceset.main.get().output)
dependsOn(configurations.runtimeClasspath)
从({
configurations.runtimeClasspath.get().filter{it.name.endsWith(“jar”)}.map{zipTree(it)}
})
}
您还需要将kotlin sdtlib打包到生成的.jar中。谢谢您的提示!只是一个简单的问题:这个java-jar.jar
命令应该如何运行?我在这里查看,它似乎必须指向java中的.class文件-与kotlin相同吗?当使用-jar
时,您需要指向.jar文件。在您的例子中,您生成了.jar。它应该像IDE一样执行jar。如果您不打印任何内容,您将无法在java-jar xxx.jar
上输出任何内容,谢谢!我想我需要先弄清楚如何创建.jar文件。如何执行kotlin jar文件,有些地方研究的jar文件对于kotlin是不可执行的。请指导我。>找不到id为“com.github.johnrengelman.shadow”的插件。