I have a multi modular Gradle project with Kotlin. One of the modules have the main method and dependencies to others. I need to create an executable jar from this module. So I have tried to do that by creating a Jar task like this
jar {
manifest {
attributes(
'Main-Class': 'my_package.RunnerKt'
)
}
from {
configurations.compileClasspath.filter{ it.exists() }.collect { it.isDirectory() ? it : zipTree(it) }
}
}
And also I tried the Shadow Gradle Plugin.
In both cases the fat jar is created as expected, it seems to have all my classes and dependencies. But when I run the jar with java -jar
I get the following error:
java.lang.NoClassDefFoundError: Could not initialize class my_kotlin_object_class
where my_kotlin_object_class
is just a kotlin object
with some variables.
In the stacktrace there are no other exceptions which could cause this one, so seems it just has a problem with this class itself. When I look into the jar, I can see this file there.
I don't understand is this a problem with kotlin object itself and executable jars, or I am doing something wrong?