Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle dependency json-simple error

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();
    }
  }
}
like image 510
April_Nara Avatar asked Jun 05 '26 08:06

April_Nara


1 Answers

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

like image 150
Michael Easter Avatar answered Jun 08 '26 00:06

Michael Easter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!