JSONObject ClassNotFoundException

偶尔善良 提交于 2019-12-04 18:25:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!