0

Im trying to compile and run by my own hands, but I have problem. Im using Intellij IDEA, Kotlin and Gradle. First, I built project (and made .class file), next, in IDE Terminal I typed "gradle build" (this make me .jar file). When Im trying to run this .jar file I got this error:

java -jar appname.jar
Exception in thread "main" java.lang.NoClassDefFoundError: sun/jvm/hotspot/HotSpotAgent
        at com.neel.helloworld.MainKt.main(main.kt:12)
        at com.neel.helloworld.MainKt.main(main.kt)
Caused by: java.lang.ClassNotFoundException: sun.jvm.hotspot.HotSpotAgent
        at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:419)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:352)
        ... 2 more

Im using AdoptOpenJDK8, PATH - user and system variable with java\bin, JAVA_HOME to home java dir.

My build.gradle.kts: (some "options" might look, but Im just trying everything)

plugins {
    kotlin("jvm") version "1.3.71"
}

group = "org.dumper"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
    jcenter()
}

dependencies {
    implementation(kotlin("stdlib-jdk8"))
    implementation(files("${System.getenv("JAVA_HOME")}\\lib\\sa-jdi.jar"))
    implementation(files("${System.getenv("JAVA_HOME")}\\lib\\tools.jar"))

    compile(kotlin("stdlib-jdk8"))
    compile(files("${System.getenv("JAVA_HOME")}\\lib\\sa-jdi.jar"))
    compile(files("${System.getenv("JAVA_HOME")}\\lib\\tools.jar"))

}

tasks {
    compileKotlin {
        kotlinOptions.jvmTarget = "1.8"
    }
    compileTestKotlin {
        kotlinOptions.jvmTarget = "1.8"
    }
}

val jar by tasks.getting(Jar::class) {
    manifest {
        attributes["Main-Class"] = "com.neel.helloworld.MainKt"
    }
}
CospriMalice
  • 13
  • 1
  • 3
  • Jar files by default don't have the dependencies inside it, therefore, when you run `java -jar appname.jar` it'll fail because it cannot find the dependencies. You need to create a FatJar to do that. Check this article https://medium.com/@preslavrachev/kotlin-basics-create-executable-kotlin-jars-using-gradle-d17e9a8384b9 – fritsMaister Apr 14 '20 at 00:19
  • Thanks, program working, but getting this error: `Exception in thread "main" java.lang.InternalError at sun.jvm.hotspot.tools.jcore.ClassWriter.writeIndex(ClassWriter.java:106) at sun.jvm.hotspot.tools.jcore.ClassWriter.writeMethod(ClassWriter.java:550) at sun.jvm.hotspot.tools.jcore.ClassWriter.writeMethods(ClassWriter.java:431) at sun.jvm.hotspot.tools.jcore.ClassWriter.write(ClassWriter.java:93) at com.neel.helloworld.MainKt.main(main.kt:20) at com.neel.helloworld.MainKt.main(main.kt)` – CospriMalice Apr 14 '20 at 00:57

1 Answers1

0

Given you are building an application, even if a CLI one, I would recommend looking into the application Gradle plugin which can take care of generating execution scripts with the proper classpath defined.

Louis Jacomet
  • 13,661
  • 2
  • 34
  • 43