I'm fairly new to Gradle so I'm trying to build a Java project and not sure about the dependencies. I have never gotten Gradle configured to be able to do my tests or now a jar file to compile and run.
My build.gradle
:
apply plugin: 'java'
apply plugin: 'maven'
repositories {
jcenter()
}
dependencies {
compile 'org.slf4j:slf4j-api:1.7.25'
compile 'org.json:json:20160212'
testCompile 'junit:junit:4.12'
}
And this is what I get on the console stating that it doesn't see my import:
error: package org.json.simple does not exist
import org.json.simple.JSONParser;
Here's my class:
import org.json.simple.*;
import java.io.*;
import java.util.*;
import java.lang.*;
public class FileLoader {
@SuppressWarnings("unchecked")
public static void main(String args[]) {
JSONParser parser = new JSONParser();
int count = 0;
try {
Object obj = parser.parse(new FileReader(
"Consumers.json"));
JSONObject jsonObject = (JSONObject) obj;
JSONArray array = jsonObject.getJSONArray("people");
} catch (Exception e) {
e.printStackTrace();
}
}
}
If you download the JSON jar specified, and list its contents (e.g. with jar tf
), it does not contain the org.json.simple
package.
So the problem is simply that you need another jar.
EDIT:
I don't know if this is the intent, but an educated guess: if you add this dependency to build.gradle
:
compile 'com.googlecode.json-simple:json-simple:1.1.1'
and these imports:
import org.json.simple.parser.*;
// import org.json.simple.*;
import org.json.*;
then the example compiles (for me).
Adding this to my build.gradle file works:
implementation 'com.googlecode.json-simple:json-simple:1.1.1'
You don't have the correct dependency to use org.json.simple
libraries.
I think you may want the coordinates for a dependency like https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple , but it is not easy to find the Maven coordinates.
If you wanted to use that library you could add these parts to your build script:
repositories {
jcenter()
}
dependencies {
compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'
}
Add this to the file to fix imports:
import org.json.simple.parser.*;
Then, you just have to fix the usage errors in your class definition.
Also, that library looks unmaintained so you may want to explore other JSON parsing libraries.