18

I am working in IntelliJ and using Maven. I have a class that uses JSONObject:

import org.json.JSONObject;

try {
  JSONObject documentObj = new JSONObject(document);
} catch (Exception e) {
  throw new RuntimeException("Failed to convert JSON String to JSON Object.", e);
}

Maven dependency in the pom.xml file:

<dependency>
  <groupId>org.json</groupId>
  <artifactId>json</artifactId>
  <version>20090211</version>
</dependency>

I can do a mvn clean package and builds successfully. But when I try to run it, I get:

Error: java.lang.ClassNotFoundException: org.json.JSONObject

Is there anything else I'm missing here? Thanks!

ℛɑƒæĿᴿᴹᴿ
  • 4,983
  • 4
  • 38
  • 58
kimmii12
  • 183
  • 1
  • 1
  • 4
  • 1
    Did you checked that jar is in your classpath or in the war file ? – NullPointerException Apr 11 '13 at 14:15
  • I have my project in intelliJ, and I run it using a bash script from the command line in linux. – kimmii12 Apr 17 '13 at 20:33
  • Alright, I got it--y'all were right, I didn't have it in my classpath. There were two variables set in the bash script: LIBJARS, the jars required by the job, and the CLASSPATH. I set it in the CLASSPATH, but in fact it was needed in the LIBJARS variable.... the joys of taking over others' code. :) Thanks for your help! – kimmii12 Apr 17 '13 at 20:53

4 Answers4

39

Add json jar to your classpath

or use java -classpath json.jar ClassName

Or add this to your maven pom.xml depedencies:

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20090211</version>
</dependency>
Alya'a Gamal
  • 5,624
  • 19
  • 34
  • Alright, I got it--y'all were right, I didn't have it in my classpath. There were two variables set in the bash script: LIBJARS, the jars required by the job, and the CLASSPATH. I set it in the CLASSPATH, but in fact it was needed in the LIBJARS variable.... the joys of taking over others' code. :) Thanks for your help! – kimmii12 Apr 17 '13 at 20:53
  • This is the latest dependency & it don't seems to have "import org.json.JSONObject;". Could you please guide ASAP? com.google.code.gson gson 2.3.1 – PAA May 01 '15 at 11:09
10

As of today (15th July 2020), you need to use latest maven repository as per below:

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20200518</version>
</dependency>

For any new version in the future, you can check it here:

Then simply replace 20200518 with latest new version value.

ℛɑƒæĿᴿᴹᴿ
  • 4,983
  • 4
  • 38
  • 58
Jerry Chong
  • 7,954
  • 4
  • 45
  • 40
5

Using the latest maven dependency solved the issue for me

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20171018</version>
</dependency>
Akhil Jain
  • 13,872
  • 15
  • 57
  • 93
2

Originally I was using net.sf.json-lib but it was creating issues with JSONArray whenever I try to use .put() method

Following solved my issue:

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>LATEST</version>
</dependency>

Although I am not big fan of using LATEST but since its a numeric version number, instead of more understndable versioning, so I found this one working for me.

user2451016
  • 1,857
  • 3
  • 20
  • 44