ClassNotFound error in Gradle for Java project

madu :

I am new to Gradle and trying to build a JAR of a simple app. This is my main file:

package com.bo.getserversecrets;

import org.json.JSONObject;
import org.json.JSONException;

public class GetServerSecrets {

    public static void main(String[] args) {
        getServerSecrets();
    }

    public static void getServerSecrets() {

        JSONObject jsonObject = new JSONObject("JSON String");

    }
}

I am trying to work with AWS SDK but having problems even at this. This is my build.gradle:

plugins {
    id 'java'
}

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

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'

    implementation 'org.json:json:20180813'
}

jar {
    manifest {
        attributes 'Main-Class': 'com.bo.getserversecrets.GetServerSecrets'
    }
}

In my Gradle run configuration I have build jar. The build succeeds and the JAR file is generated. However when I run with the JAR (java -jar), I get this error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONObject
        at com.bo.getserversecrets.GetServerSecrets.getServerSecrets(GetServerSecrets.java:34)
        at com.bo.getserversecrets.GetServerSecrets.main(GetServerSecrets.java:20)
Caused by: java.lang.ClassNotFoundException: org.json.JSONObject
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)

Any suggestions to try out are really appreciated. Thank you.

Daniele :

As usual, NoClassDefFoundError: org/json/JSONObject means that your Java code compiled using the org.json.JSONObject class; but that class is not present at runtime. (and since the class is missing, the whole jar is probably missing).

You need to make sure the org.json:json:20180813 dependency is present in the classpath when you run your Java application.

There are many ways to do that, either you collect all dependencies and put them in a lib folder; or you can create an uber-jar.


Creating an uber-jar

In example, you can create an uber-jar using the gradle-shadow plugin (see github-gradle-shadow ).

See below the updated build script; it builds with gradle clean shadowJar and runs with java -jar build/libs/com.bo-1.0-SNAPSHOT-all.jar

plugins {
    id 'java'
    id 'com.github.johnrengelman.shadow' version '5.0.0'
}
group 'com.bo'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
    mavenCentral()
}
dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    implementation 'org.json:json:20180813'
}
shadowJar {
    baseName = project.group
    version = project.version    
    manifest {
        attributes 'Main-Class': 'com.bo.getserversecrets.GetServerSecrets'
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=156326&siteId=1