简体   繁体   English

无法使用Gradle和Kotlin运行生成的JAR文件

[英]Can't run generated JAR file with Gradle and Kotlin

I have a project configured with Gradle and Kotlin. 我有一个使用Gradle和Kotlin配置的项目。 It's a command line utility and I would like to be able to run the generated jar from my terminal. 这是一个命令行实用程序,我希望能够从我的终端运行生成的jar。 However I get the following error: 但是我收到以下错误:

Exception in thread "main" java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics
    at com.autentia.impt.MainKt.main(Main.kt)
Caused by: java.lang.ClassNotFoundException: kotlin.jvm.internal.Intrinsics
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more

My gradle configuration is as follows: 我的gradle配置如下:

buildscript {
    ext.kotlin_version = '1.2.20'
    ext.junit_platform_version = '1.0.1'
    ext.junit_version = '5.0.0'
    ext.moshi_version = '1.5.0'
    ext.jna_version = '4.5.0'

    repositories {
        mavenCentral()
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.junit.platform:junit-platform-gradle-plugin:$junit_platform_version"
    }
}

version '1.0-SNAPSHOT'

apply plugin: 'kotlin'
apply plugin: 'application'
apply plugin: 'org.junit.platform.gradle.plugin'

mainClassName = 'com.autentia.impt.MainKt'

repositories {
    mavenCentral()
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib"
    implementation "org.jetbrains.kotlin:kotlin-reflect"
    implementation "com.squareup.moshi:moshi:$moshi_version"
    implementation "com.squareup.moshi:moshi-kotlin:$moshi_version"
    implementation "net.java.dev.jna:jna:$jna_version"
    testImplementation("org.junit.jupiter:junit-jupiter-api:$junit_version")
    testRuntime("org.junit.jupiter:junit-jupiter-engine:$junit_version")
}

sourceSets {
    main.kotlin.srcDirs += 'src/main/kotlin'
}

task wrapper(type: Wrapper) {
    gradleVersion = '4.4.1'
}

jar {
    manifest {
        attributes "Main-Class": mainClassName
    }

    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

The command I use to generate the jar is ./gradlew clean build and the command I use to run the jar is java -jar build/libs/impt-1.0-SNAPSHOT.jar . 我用来生成jar的命令是./gradlew clean build ,我用来运行jar的命令是java -jar build/libs/impt-1.0-SNAPSHOT.jar

I've tried following the official docs and also I've tried with these resources: 1 , 2 and 3 without any luck. 我试过下面的官方文档 ,也我与这些资源的尝试: 123没有任何的运气。

Just change implementation configurations to compile . 只需更改implementation配置即可compile (At least for kotlin-stdlib) (至少对于kotlin-stdlib)

The compile configuration is deprecated and should be replaced by implementation or api in Gradle plugin for Android, however for kotlin-gradle-plugin , I think you still need compile . 不推荐使用compile配置,应该用Android的Gradle插件中的implementationapi替换,但是对于kotlin-gradle-plugin ,我认为你仍然需要编译

Alright I had the same problem and finally figured this out: 好吧我有同样的问题,最后想出来:

My main class name was Main.kt and this is what I had in my build.gradle file when I was getting the same error: 我的主要类名是Main.kt ,这是我在build.gradle文件中遇到同样的错误:

jar {
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    manifest {
        attributes(
                'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
                'Main-Class': "play.Main"
        )
    }
}

Although I noticed when I create the jar artifact in Intellij, it uses play.MainKt as my Main-Class name (yeah, weird naming convention), then I changed my build.gradle file to this: 虽然我注意到当我在Intellij中创建jar工件时,它使用play.MainKt作为我的Main-Class名称(是的,奇怪的命名约定),然后我将build.gradle文件更改为:

jar {
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    manifest {
        attributes(
                'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
                'Main-Class': "play.MainKt"
        )
    }
}

and now I am not getting that error. 现在我没有得到那个错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM