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...
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 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>
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.
Using the latest maven dependency solved the issue for me
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20171018</version>
</dependency>
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.