Gradle : java.lang.ClassNotFoundException : When running Uber Jar

513 Views Asked by At

I am trying to build uber jar in java with gradle version 6.0 and it builds fine. But when i run Uber Jar, it throws me following error

root@9393134c8941:/home/gradle/checkout-service# java -jar build/libs/checkout-service-1.0-SNAPSHOT-uber.jar
Error: Could not find or load main class com.noths.runner.Runner
Caused by: java.lang.ClassNotFoundException: com.noths.runner.Runner

My build.gradle file looks like below

// Shadow Plugin https://github.com/johnrengelman/shadow
plugins {
    id 'java'
}

group 'com.noths'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'

    compileOnly 'org.projectlombok:lombok:1.18.20'
    annotationProcessor 'org.projectlombok:lombok:1.18.20'

    testCompileOnly 'org.projectlombok:lombok:1.18.20'
    testAnnotationProcessor 'org.projectlombok:lombok:1.18.20'

    implementation 'com.google.code.gson:gson:2.8.8'

    implementation group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
    testImplementation group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.25'

    implementation group: 'ch.qos.logback', name: 'logback-core', version: '1.2.3'
    testImplementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
}

test {
    useJUnitPlatform()
}

task uberJar(type: Jar) {
    manifest {
        attributes 'Main-Class': 'com.noths.runner.Runner'
    }
    archiveClassifier = 'uber'

    from sourceSets.main.output

    dependsOn configurations.runtimeClasspath
    from {
        configurations.runtimeClasspath.findAll { it.name.endsWith('jar') }.collect { zipTree(it) }
    }
}

I am building the uber jar with follwing steps

cd /home/gradle/checkout-service/
rm -rf .gradle
gradle clean
gradle build --refresh-dependencies
gradle uberJar;

Uber Jars as in the output

root@9393134c8941:/home/gradle/checkout-service# ls -lh build/libs/
total 736K
-rw-r--r-- 1 root root  261 Aug 25 05:07 checkout-service-1.0-SNAPSHOT.jar
-rw-r--r-- 1 root root 729K Aug 25 05:08 checkout-service-1.0-SNAPSHOT-uber.jar

I opened the uber jar as well and it has Runner class located at the same path as given in build.gradle.

Can anyone tell what am i missing ?

1

There are 1 best solutions below

0
On

There are various things here that are amiss:

  1. The reason why it is unable to find the main class of your project could be attributed to the fact that there is no manifest declaring it.

  2. You mention that your are using the Gradle shadow Jar plugin (assuming this one). Even though you do that, it seems that you define a custom jar task to create the fat jar.

Based on all the above, I would do the following:

  1. Get rid of the custom task that is redundant
  2. Have a look at the documentation of ShadowJar here in order to properly understand how to set it up.