I had a running build.gradle which allowed me to build jar packages. All of a sudden it stopped working, when trying to run a built jar the jvm claims that the main class could not be found. I didn't change anything from the file. The manifest inside the jar is correct and Main.class also there. I don't understand why it doesn't find the class anymore.
Error: Could not find or load main class Main Caused by: java.lang.ClassNotFoundException: Main
plugins {
id 'java'
// id 'application'
}
repositories {
mavenCentral()
}
jar {
duplicatesStrategy(DuplicatesStrategy.EXCLUDE)
manifest {
attributes "Main-Class": "Main"
}
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}
dependencies { ... }
My folder structure looks as following: src --> main --> java --> Main.java
Does anyone has any idea what could have happened? I have running jars from that file, so it was working until now. Could an unnoticed gradle update lead to such situation?
I already moved the Main.java into a package but the problem still remains. I also addressed all possible combinations like main.java.Main.java, checked the manifest and if the class is inside the jar.
Spent several hours reading threads about the issue and tried out a lot of variations. None of them allowed to create working jars.
Kind regards
Finally the issue is solved. The error message was just misleading and the class could be found from the beginning on. An added dependency which was imported in the main class led to this error.
Building a shadowJar instead of jar helped me out.
plugins {
id 'java'
id 'com.github.johnrengelman.shadow' version '8.1.0'
id "idea"
}
repositories {
mavenCentral()
}
jar {
duplicatesStrategy(DuplicatesStrategy.EXCLUDE)
manifest {
attributes "Main-Class": "Main"
}
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}
dependencies { ... }