Gradle dependency json-simple error

April_Nara picture April_Nara · Oct 26, 2017 · Viewed 24k times · Source

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();
    }
  }
}

Answer

Michael Easter picture Michael Easter · Oct 26, 2017

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).