Not able to run jar file

Getting error with a very basic “hello world from jar” searched forum but not found any satisfactory solution

Exception in thread "main" java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics
        at MainfileKt.main(Mainfile.kt)
Caused by: java.lang.ClassNotFoundException: kotlin.jvm.internal.Intrinsics
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 1 more

I am using all painful tools listed as below.

Kotlin (latest 1.2.41)
Gradle (4.7)
intellij community 2018.1

in my gradle i have added additional lines (please note i am not using any package)

jar{
    manifest {
        attributes (
                'Main-Class': 'MainfileKt'
        )
    }
}

Please note my structure is

src
---->main
------------>kotlin (Please note its NOT java)
-------------------->Mainfile.kt

my Mainfile.kt

fun main(args: Array<String>) {
    println("hello world")
}

I am super irritated trouble shoot and finding online what I am doing wrong but no luck (I hope my manage will allow me go back to java sooner)

Please also note that, when I am giving class name as just Mainclass in my manifest, while running jvm says that it can not find class called Mainclass, this proves atleast JVM is able to MainclassKt

1 Like

Compiled kotlin program depends on the standard library. If you use Gradle, you might have noticed compile("org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion") dependency in build.gradle file.

So when you run the resulting jar, you either have to provide paths to your dependencies with -classpath java parameter, or — the other common approach — to pack all your dependencies in the resulting jar. The latter can be done manually or with the help of various Gradle plugins, take a look at this overview: http://www.baeldung.com/gradle-fat-jar

Also you can use the Application plugin to gather all dependencies and create a runner script.

1 Like

hi @ilya.gorbunov would you have an example for kotlin based gradle files ? .kts?

no worries found an answer here:
https://docs.gradle.org/current/userguide/working_with_files.html#sec:creating_uber_jar_example

I have followed the documentation from gradle, but instead of doing a separate fatJar (or "uberJar") task, you can just override the jar task to add all missing dependencies.

Here is the task that would go in your build.gradle.kts:

I also shared a complete build.gradle.kts with all the essentials you need, in case the above answer is not enough for you:

1 Like