简体   繁体   中英

Gradle Jar Dependency Error

I have a java program that I use Gradle to build. In this program I use the JSON jar "org.json:json:2014113" this program compiles and works just fine when I run it in my IDE (IntelliJ 14) It also compiles and runs just fine when I run the jar, except for when the function that I use the JSON in is called, I then get a Java.lang.NoClassDefFoundError: org/json/JSONArray error here is my build.gradle script

version 'UR'

apply plugin: 'java'

sourceCompatibility = 8

repositories {
    mavenCentral()
}

dependencies {

    compile group: 'org.json', name: 'json', version: '20141113'

}

jar {

    destinationDir = file("C:\\Users\\Jonah\\Documents\\Software\\RapIDE Builds")

    manifest {
        attributes 'Main-Class': 'frames.MainFrame'
    }

}

and here is my JSON class

package logic;

import frames.MainFrame;

import org.json.JSONArray;
import org.json.JSONObject;

public class JSONparser extends MainFrame
{

    public static String SLrtrn;

    public static void parseJSON(String JSONinput)
    {

        final JSONArray JSONrtrn = new JSONArray(JSONinput);

        rhymeList.removeAllElements();

        for (int i = 0; i < JSONrtrn.length(); ++i)
        {
            JSONObject parsedObj = (JSONObject) JSONrtrn.get(i);
            SLrtrn = parsedObj.get("word").toString();

            rhymeList.addElement(SLrtrn);
        }
    }
}

Am I doing something wrong in the build.gradle script?

Your Gradle script is fine but when you run the JAR, you have not included the org.json jar on the classpath .

If you don't want to have to do this, you can create what is called a fat jar which bundles the org.json JAR inside your JAR. Such a process is described here .

There are many other ways to do this (see here and here )so you should research the different options but at the end of the day the org.json JAR needs to be present when you compile, but also when you run the program. At the minute you are only fulfilling the first of those requirements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM