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
  • 1 App.class must be in the w/x/y/z folder hierarchy (w must be under the root of the JAR) - also try java -cp file.jar w.x.y.z.App – user85421 Commented Nov 20, 2024 at 9:56
  • @user85421 w is well at the root of the jar and java -cp file.jar w.x.y.z.App returns the same error (Error: Could not find or load main class w.x.y.z.App) – Nix Commented Nov 20, 2024 at 10:12
  • 1 shouldn't it be public class App ? – Gyro Gearless Commented Nov 20, 2024 at 10:18
  • @GyroGearless That's true ! But I corrected it and it changes nothing. I edit the post – Nix Commented Nov 20, 2024 at 10:19
  • Copy the command line and error literally from the command line, with all lines, as text, as paste it in your question using code block format. – aled Commented Nov 20, 2024 at 11:18
 |  Show 3 more comments

1 Answer 1

Reset to default 0

For 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