1552980358
1552980358

Reputation: 73

My output .jar gives an java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics

The project can be run with 'Run' -> 'Run\'[PACKAGE.NAME].MainKt\'' in Intellij Idea IDE.

However, the I have tried several methods from Stack Overflow, the problem still exists as well as I built jar file, and when run the output jar file jar -jar [JarFilePathInMyComputer].JarFile.jar ,it throws an Exception as below

 Exception in thread "main" java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics
        at [PACKAGENAME].MainKt.main(Main.kt)
Caused by: java.lang.ClassNotFoundException: kotlin.jvm.internal.Intrinsics
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:190)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499)
        ... 1 more

My dependencies are

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
    implementation 'org.jetbrains.kotlin:kotlin-runtime:1.2.71'
    implementation "com.google.code.gson:gson:2.8.5"
    implementation 'org.apache.httpcomponents:httpclient:4.5.9'
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

When I add artifacts, OutPut layout contains below kotlin libraries

Gradle: commons-code:commons-codec:1.11
Gradle: org.jetbrains.kotlin-stdlib-jdk8-1.3.21
Gradle: org.jetbrains.annaotations:13.0
Gradle: juniy.junit:4.12
Gradle: org.jetbrains.kotlin:kotlin-stdlib:1.3.21
Gradle: org.jetbrains.kotlin-runtime:1.2.71
Gradle: org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.21
Gradle: org.jetbrains.kotlin:kotlin-stdlib-common:1.3.21

My MANIFEST.MF us

Manifest-Version: 1.0
Main-Class: [PACKAGE.NAME].MainKt
Class-Path: [PACKAGE.NAME]

How can I fix it? I have tried several methods, but the same exception still throws.

Upvotes: 1

Views: 2520

Answers (1)

npskirk
npskirk

Reputation: 1188

You need to either add the kotlin runtime to your classpath when invoking java, or you need to build a fat jar file that packages the runtime in your jar file. Personally, I like the fat jar option the best using the shadow plugin.

plugins {
    ...
    id "com.github.johnrengelman.shadow"
}

or you can use the application plugin, that creates a deployable package with windows and unix startup scripts that manage the classpath.

plugins {
    ...
    id "application"
}

There are some other approaches to creating a fat jar given in the answers to this question (as Andreas suggested), and the kotlin examples repository has several projects that use the application plugin.

Upvotes: 1

Related Questions