javajaruberjar

Application works but "Could not find or load main class" when launching jar


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'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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ...

public class App {

  LoggerFactory.getLogger(App.class);

  public static void main(String[] args) {
    LOG.info("===== START =====");
    ... 
  }
}

Solution

  • 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