I'm trying to create and run an executable jar through gradle. This is what my current gradle looks like:
task jarTask(type: Jar) {
baseName = 'my-main-class'
from 'build/classes/main'
}
task createJarWithDependencies(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Sample Jar',
'Implementation-Version': 1,
'Main-Class':'com.example.MyMainClass'
}
baseName = "my-main-class-with-dependencies"
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
}
with jarTask
}
configurations {
jarConfiguration
}
artifacts {
jarConfiguration jarTask
}
// This is the task that I call with ./gradlew to execute my jar
task runMyJar(type: JavaExec) {
classpath files('build/libs/my-main-class-with-dependencies.jar')
main 'com.example.MyMainClass'
args = ["param1","param2"]
outputs.upToDateWhen { false }
}
runMyJar.dependsOn(createJarWithDependencies, build)
I got these approach from the following stack overflow answers/references below: Android Studio export jar with dependencies Android studio - How to use an executable jar file from gradle
However when I run ./gradlew clean runMyJar
(or even just ./gradlew runMyJar
, I get the following error message:
Error: Could not find or load main class com.example.MyMainClass
Can anyone point out the reason why my executable jar is not finding the main method inside my class? Is there anything I'm missing?