6

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.

César Alberca
  • 2,321
  • 2
  • 20
  • 32
  • it seems you don't have certain dependencies on the classpath. Is it possible you had these dependencies on the path of your IDE, instead of on that of your machine itself? – Stultuske Jan 22 '18 at 11:15
  • Same problem! Did you figure this out? – Yar Jun 18 '18 at 02:48

2 Answers2

5

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.

Erkut Evirgen
  • 576
  • 4
  • 9
-1

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:

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:

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.

Yar
  • 7,020
  • 11
  • 49
  • 69