Gradle - FatJar - Could not find or load main class

Hadrien picture Hadrien · Jul 21, 2018 · Viewed 10.1k times · Source

I know that question was asked a lot and has many answers, but i still get it and I don't understand why...

I am trying to generate a .jar from a projet with dependencies with gradle.

I have a class src/main/java/Launcher.java, in which I have my main method.

there is my build.gradle

plugins {
    id 'java'
    id 'application'
}

version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
mainClassName = 'Launcher'

repositories {
    mavenCentral()
}

dependencies {
    compile 'commons-io:commons-io:2.1'
    compile 'io.vertx:vertx-core:3.4.0'
    compile 'io.vertx:vertx-web:3.4.0'
    compile 'com.google.code.gson:gson:1.7.2'
    compile "com.auth0:java-jwt:3.1.0"
    compile 'org.mongodb:mongo-java-driver:3.4.1'
    compile 'com.google.guava:guava:24.1-jre'
    compile 'commons-io:commons-io:2.6'
}

jar {
    manifest {
        attributes "Main-Class": mainClassName
    }

    from {
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

I use $>gradle assemble to generate my jar then $>java -jar path/to/my/.jar And i get the error "could not find or load main class Launcher"...

I dont understand why, when I look in the .jar, I have Launcher class and in META-INF I have my manifest

screenshot

Sorry for still asking this question in 2018 but i'm loosing my mind trying to figure out what's wrong. I hope somone will have the answer !

Answer

avolkmann picture avolkmann · Jul 21, 2018

I reproduced your issue locally.

Just add exclude 'META-INF/*.RSA', 'META-INF/*.SF', 'META-INF/*.DSA' to the jar task.

This will exclude the signatures of interfering dependencies.

Example:

jar {
    manifest {
        attributes "Main-Class": mainClassName
    }

    from {
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    }
    exclude 'META-INF/*.RSA'
    exclude 'META-INF/*.SF'
    exclude 'META-INF/*.DSA'
}