I have a full stack Kotlin Multiplatform web app with Kotlin/JVM backend and Kotlin/JS frontend.
The problem I'm having is that, when I go to execute the JAR, there is no Main-Class
manifest entry and I get this error:
$ java -jar shoppinglist-jvm-1.0-SNAPSHOT.jar
no main manifest attribute, in shoppinglist-jvm-1.0-SNAPSHOT.jar
I assume that this is, for some reason, by design and that I'm missing something essential.
Further Details:
jvm
JAR in the libs folder, but I also attempted it on both the js
and metadata
JARs with the same result.gradle build
within IntelliJ Run Configurations to compile.Extracting the JARs with 7-Zip reveals that js
and metadata
don't contain Java bytecode; so, I'm, mostly, ignoring those for now.
However, jvm
has interesting contents:
Main-Class
.I'm thinking this is something simple that I'm missing, but I can't seem to find documentation or questions about this specifically anywhere.
# Build from the root dir
./gradlew clean build
cd build/distributions/
# unzip
tar -xvf shoppinglist-1.0-SNAPSHOT.tar
cd shoppinglist-1.0-SNAPSHOT/bin
# Execute launch script (generated by application plugin)
./shoppinglist
# Result
Hello, JVM!
We need to add our custom distribution and edit manifest if you really want to execute it like java -jar shoppinglist-jvm-1.0-SNAPSHOT.jar
application
plugin with distribution
pluginapplication
configuration blocktasks.getByName<JavaExec>("run") {
classpath(tasks.getByName<Jar>("jvmJar")) // so that the JS artifacts generated by `jvmJar` can be found and served
}
distribution
configuration block withdistributions {
main {
distributionBaseName.set("shoppinglist")
contents {
into("") {
val jvmJar by tasks.getting
from(jvmJar)
}
into("lib/") {
val main by kotlin.jvm().compilations.getting
from(main.runtimeDependencyFiles)
}
}
}
}
tasks.withType<Jar> {
doFirst {
manifest {
val main by kotlin.jvm().compilations.getting
attributes(
"Main-Class" to "ServerKt",
"Class-Path" to main.runtimeDependencyFiles.files.joinToString(" ") { "lib/" + it.name }
)
}
}
}
Now you can launch it like so
# Build from the root dir
./gradlew clean build
cd build/distributions/
# unzip
tar -xvf shoppinglist-1.0-SNAPSHOT.tar
# Run
java -jar shoppinglist-jvm-1.0-SNAPSHOT.jar
# Result
Hello, JVM!
1. Remove `applicaion` plugin
2. Remove `distributions` and `application` configuration blocks
3. Remove `stage` and `run` tasts
4. Add uberJar task
tasks.withType<Jar> {
doFirst {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
val main by kotlin.jvm().compilations.getting
manifest {
attributes(
"Main-Class" to "ServerKt",
)
}
from({
main.runtimeDependencyFiles.files.filter { it.name.endsWith("jar") }.map { zipTree(it) }
})
}
}