Hello, I got this error after try to run generated...
# announcements
d
Hello, I got this error after try to run generated jar any idea ?
Copy code
➜  life-drain git:(master) ✗ ./gradlew build && java -jar build/libs/life-drain-1.0-SNAPSHOT.jar
w: /home/kotl/repo/watchanimeonline/life-drain/src/main/kotlin/com/life/drain/libs/GetMovieUrls.kt: (65, 33): Unnecessary non-null assertion (!!) on a non-null receiver of type List<String>

BUILD SUCCESSFUL in 3s
6 actionable tasks: 6 executed
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: kotlin/jvm/functions/Function1
	at java.lang.Class.getDeclaredMethods0(Native Method)
	at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
	at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
	at java.lang.Class.getMethod0(Class.java:3018)
	at java.lang.Class.getMethod(Class.java:1784)
	at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
	at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: kotlin.jvm.functions.Function1
	at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	... 7 more
Here's my
build.gradle
Copy code
plugins {
    id 'java'
    id 'org.jetbrains.kotlin.jvm' version '1.3.0'
}

group 'life-drain'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

apply plugin: 'application'
mainClassName = "com.life.drain.Main"

sourceSets {
    main {
        kotlin {
            srcDir 'src'
        }
    }
}

jar {
    manifest {
        attributes "Main-Class": "$mainClassName"
    }
    from {
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

task sitemapGen(type: JavaExec) {
    main = 'com.life.drain.scripts.sitemapGenerator.Main'
    classpath = sourceSets.main.runtimeClasspath
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
    implementation "org.jsoup:jsoup:latest.release"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:latest.release"
    implementation "org.litote.kmongo:kmongo-coroutine:latest.release"
    implementation "com.github.slugify:slugify:latest.release"
    implementation "com.google.code.gson:gson:latest.release"
    implementation "com.github.dfabulich:sitemapgen4j:latest.release"
}

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
Here's the folder structure
m
Try adding Kt to your main class name e.g. mainClassName = "com.life.drain.MainKt"
c
That
java -jar
command is only supplying the main jar, but none of its dependencies. Why not use the Gradle Application plugin or a JavaExec task https://docs.gradle.org/current/userguide/application_plugin.html https://docs.gradle.org/current/dsl/org.gradle.api.tasks.JavaExec.html
d
@MrNiamh not working 😭
@Casey Brooks I want to create executable jar to be used at other application
d
If you really need a single exectuable jar (and not something like a tar or zip file), the keyword to look for is "far jar", you should be able to find many instructions on google.
c
Then you’ll have to make a “fat jar” for the result to be fully executable. Gradle won’t create it by default, the output jar just contains the code in your project. It expects that your jar will be combined with other jars at runtime, but a fatjar will merge all those jars into a single one for you so that you don’t need the others at runtime. Check out this tutorial for getting this set up https://www.baeldung.com/gradle-fat-jar
d
@Casey Brooks actually I already follow that tutorial, and resulting to this error at post 😭
s
Copy code
dependencies {
    compile(kotlin("stdlib-jdk8"))
    compile(kotlin("reflect"))
    compile("org.jetbrains.kotlinx:kotlinx-coroutines-core:0.30.2-eap13")
    compile("org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:0.30.2-eap13")
    compile("org.jetbrains.kotlinx:kotlinx-coroutines-javafx:0.30.2-eap13")
    compile("no.tornado:tornadofx:1.7.17")
    compile("de.jensd:fontawesomefx:8.9")
    compile("org.controlsfx:controlsfx:8.40.14")
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        jvmTarget = "1.8"
        freeCompilerArgs = listOf("-XXLanguage:+InlineClasses")
    }
}

tasks.withType<Jar> {
    manifest {
        attributes["Main-Class"] = mainClass
    }
}

val fatJar = task("fatJar", type = Jar::class) {
    baseName = "${project.name}-fat"
    manifest {
        attributes["Main-Class"] = mainClass
    }
    from(configurations.runtime.map { if (it.isDirectory) it else zipTree(it) })
    with(tasks["jar"] as CopySpec)
}
I am far from a gradle kts expert but I have this one which works, I use the runtime config to produce fat jar and have compile dependencies, you have collected the compile config and have runtime dependencies, maybe the compile carries forward to the runtime but not vice versa 🤷🏻‍♂️
d
I already solved my problem using shadowJar plugin. hmm