Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSONObject ClassNotFoundException

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!

like image 541
kimmii12 Avatar asked Apr 11 '13 14:04

kimmii12


3 Answers

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>
like image 96
Alya'a Gamal Avatar answered Oct 30 '22 02:10

Alya'a Gamal


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:

  • https://mvnrepository.com/artifact/org.json/json

Then simply replace 20200518 with latest new version value.

like image 9
Jerry Chong Avatar answered Oct 30 '22 02:10

Jerry Chong


Using the latest maven dependency solved the issue for me

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20171018</version>
</dependency>
like image 5
Akhil Jain Avatar answered Oct 30 '22 02:10

Akhil Jain