Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't run generated JAR file with Gradle and Kotlin

I have a project configured with Gradle and Kotlin. It's a command line utility and I would like to be able to run the generated jar from my terminal. 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:

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.

I've tried following the official docs and also I've tried with these resources: 1, 2 and 3 without any luck.

like image 590
César Alberca Avatar asked Jan 22 '18 11:01

César Alberca


People also ask

How do I run Gradle Kotlin project?

From inside the new project directory, run the init task using the following command in a terminal: gradle init . When prompted, select the 2: application project type and 4: Kotlin as implementation language. Next you can choose the DSL for writing buildscripts - 1 : Groovy or 2: Kotlin .

How do you fix your project requires a newer version of the kotlin gradle plugin?

Solution of This error:Go to the Kotlin Gradle Plugin version checkup page, and get the latest version from the page. The latest version we get is "1.6. 21", now you have to update the version code at build. gradle located at android\build.

Does Gradle build create jar?

The result JAR will be created in build/libs/ directory by default.


1 Answers

Just change implementation configurations to compile. (At least for 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.

like image 126
Erkut Evirgen Avatar answered Oct 12 '22 12:10

Erkut Evirgen