问题
I am working in IntelliJ and using maven. I have a class that uses JSONObject, and I have imported it
import org.json.JSONObject;
and in a method I use it like so:
JSONObject documentObj = null;
try {
documentObj = new JSONObject(document);
} catch (Exception e) {
throw new RuntimeException("Failed to convert JSON String document into a JSON Object.", e);
}
I also have the dependency in the pom.xml file
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
I can "mvn clean package" this code and everything 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!
回答1:
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>
回答2:
Using the latest maven dependency solved the issue for me
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20171018</version>
</dependency>
来源:https://stackoverflow.com/questions/15951032/jsonobject-classnotfoundexception