JSONObject ClassNotFoundException

kimmii12 picture kimmii12 · Apr 11, 2013 · Viewed 74.9k times · Source

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!

Answer

Alya&#39;a Gamal picture Alya'a Gamal · Apr 11, 2013

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>