admin管理员组文章数量:1334825
It's time for one of the theoretically most basic question ! I tried all classic and simple solutions and I'm running out of ideas.
So, my App works well when I launch it directly in my IDE but when I try to execute the fatJar (with a classical java -jar locationOfJar) I get the traditionnal "Error: Could not find or load main class w.x.y.z.App"
What I tried :
I quadruple checked that my main class is well defined in my build.gradle.kts (I give parts later) I even wrote this post with real copy/paste and changed all with "replace" to check if they are really the same name
I unzipped my jar and checked that the main-class is there with the right name
in the unzipped jar I checked that every import of the main-class is present
I verified that the .jar contains a META-INF/MANIFEST.MF, it contains 2 lines : Manifest-Version: 1.0 and Main-Class: w.x.y.z.App
when I try to run the jar (and not the fatJar) I get the error "no main manifest attribute" while the manifest is there too !
I'm sure it's a stupid mistake but I can't find it ! Do you have an idea ?
My build.gradle.kts :
plugins {
id("java")
}
java {
sourceCompatibility = JavaVersion.VERSION_17
}
tasks.register<Jar>("fatJar") {
group = "build"
archiveClassifier.set("all")
from(sourceSets.main.get().output)
dependsOn(configurations.runtimeClasspath)
from({
configurations.runtimeClasspath.get().map { if (it.isDirectory) it else zipTree(it) }
})
manifest {
attributes["Main-Class"] = "w.x.y.z.App"
}
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
dependencies {
...
}
repositories {
mavenCentral()
maven {
name = "Artifact"
url = uri(project.findProperty("mavenRepositoryUrl") as String)
credentials {
username = project.findProperty("mavenRepositoryUsername") as String
password = project.findProperty("mavenRepositoryPassword") as String
}
}
}
Start of App Class :
package w.x.y.z;
import .slf4j.Logger;
import .slf4j.LoggerFactory;
import ...
public class App {
LoggerFactory.getLogger(App.class);
public static void main(String[] args) {
LOG.info("===== START =====");
...
}
}
It's time for one of the theoretically most basic question ! I tried all classic and simple solutions and I'm running out of ideas.
So, my App works well when I launch it directly in my IDE but when I try to execute the fatJar (with a classical java -jar locationOfJar) I get the traditionnal "Error: Could not find or load main class w.x.y.z.App"
What I tried :
I quadruple checked that my main class is well defined in my build.gradle.kts (I give parts later) I even wrote this post with real copy/paste and changed all with "replace" to check if they are really the same name
I unzipped my jar and checked that the main-class is there with the right name
in the unzipped jar I checked that every import of the main-class is present
I verified that the .jar contains a META-INF/MANIFEST.MF, it contains 2 lines : Manifest-Version: 1.0 and Main-Class: w.x.y.z.App
when I try to run the jar (and not the fatJar) I get the error "no main manifest attribute" while the manifest is there too !
I'm sure it's a stupid mistake but I can't find it ! Do you have an idea ?
My build.gradle.kts :
plugins {
id("java")
}
java {
sourceCompatibility = JavaVersion.VERSION_17
}
tasks.register<Jar>("fatJar") {
group = "build"
archiveClassifier.set("all")
from(sourceSets.main.get().output)
dependsOn(configurations.runtimeClasspath)
from({
configurations.runtimeClasspath.get().map { if (it.isDirectory) it else zipTree(it) }
})
manifest {
attributes["Main-Class"] = "w.x.y.z.App"
}
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
dependencies {
...
}
repositories {
mavenCentral()
maven {
name = "Artifact"
url = uri(project.findProperty("mavenRepositoryUrl") as String)
credentials {
username = project.findProperty("mavenRepositoryUsername") as String
password = project.findProperty("mavenRepositoryPassword") as String
}
}
}
Start of App Class :
package w.x.y.z;
import .slf4j.Logger;
import .slf4j.LoggerFactory;
import ...
public class App {
LoggerFactory.getLogger(App.class);
public static void main(String[] args) {
LOG.info("===== START =====");
...
}
}
Share
Improve this question
edited Nov 20, 2024 at 10:20
Nix
asked Nov 20, 2024 at 9:29
NixNix
4516 silver badges13 bronze badges
8
|
Show 3 more comments
1 Answer
Reset to default 0For those wondering, the problem was solved by adding "exclude("META-INF/.SF", "META-INF/.DSA", "META-INF/*.RSA")" in the tasks.register("fatJar")
I was using a dependency that generated those kind of files next to the manifest and that was making the mess
本文标签: javaApplication works but quotCould not find or load main classquot when launching jarStack Overflow
版权声明:本文标题:java - Application works but "Could not find or load main class" when launching jar - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742368866a2461824.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
App.class
must be in thew/x/y/z
folder hierarchy (w
must be under the root of the JAR) - also tryjava -cp file.jar w.x.y.z.App
– user85421 Commented Nov 20, 2024 at 9:56public class App
? – Gyro Gearless Commented Nov 20, 2024 at 10:18