java.lang.NoClassDefFoundError: org/json/JSONObject

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27504508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 04:28:23  来源:igfitidea点击:

java.lang.NoClassDefFoundError: org/json/JSONObject

javajsonservlets

提问by HitchHiker

I am using Eclipse IDE and am writing a servlet. The servlet should accept values from an html file and return JSON response accordingly.

我正在使用 Eclipse IDE 并且正在编写一个 servlet。servlet 应该接受来自 html 文件的值并相应地返回 JSON 响应。

My doPost() is:

我的 doPost() 是:

protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

        try
        {
        res.setContentType("application/json");
        res.setHeader("Cache-Control", "nocache");
            res.setCharacterEncoding("utf-8");

        PrintWriter out = res.getWriter();

        JSONObject json = new JSONObject();

        String un=req.getParameter("uname");
        String pw=req.getParameter("password");

        if(un.equals("xxx") && pw.equals("xxx"))            
            json.put("result", "success");
        else
            json.put("result", "fail");

        out.print(json.toString());
        }
        catch(Exception e){System.out.print( e.getMessage());}  
    }

When I run this servlet in Eclipse I get a file download dialog.

当我在 Eclipse 中运行这个 servlet 时,我得到一个文件下载对话框。

When run outside Eclipse with Tomcat, I get error:

使用 Tomcat 在 Eclipse 外部运行时,出现错误:

root cause

java.lang.NoClassDefFoundError: org/json/JSONObject
    Server1.doPost(Server1.java:25)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

root cause

java.lang.ClassNotFoundException: org.json.JSONObject
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1713)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1558)
    Server1.doPost(Server1.java:25)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

The line Server1.doPost(Server1.java:25) refers to

行 Server1.doPost(Server1.java:25) 指的是

JSONObject json = new JSONObject();

I have added the org.json.jar to the build path as well as added it to deployment path in Properties->Configure build path->Deployment assembly

我已将 org.json.jar 添加到构建路径中,并将其添加到部署路径中 Properties->Configure build path->Deployment assembly

采纳答案by Jaya Ananthram

No.. It is not proper way.Refer the steps,

不。。这不是正确的方法。参考步骤,

For Classpath reference:Right click on project in Eclipse -> Buildpath -> Configure Build path -> Java Build Path (left Pane) -> Libraries(Tab) -> Add External Jars -> Select your jar and select OK.

For Classpath reference:在 Eclipse 中右键单击项目 -> Buildpath -> Configure Build path -> Java Build Path(左窗格)-> Libraries(Tab) -> Add External Jars -> 选择你的 jar 并选择 OK。

For Deployment Assembly:Right click on WAR in eclipse-> Buildpath -> Configure Build path -> Deployment Assembly (left Pane) -> Add -> External file system -> Add -> Select your jar -> Add -> Finish.

For Deployment Assembly:在 Eclipse 中右键单击 WAR-> 构建路径 -> 配置构建路径 -> 部署程序集(左窗格)-> 添加 -> 外部文件系统 -> 添加 -> 选择您的 jar -> 添加 -> 完成。

This is the proper way! Don't forget to remove environment variable. It is not required now.

这是正确的方法!不要忘记删除环境变量。现在不需要了。

Try this. Surely it will work. Try to use Maven, it will simplify you task.

尝试这个。肯定会奏效的。尝试使用 Maven,它会简化您的任务。

回答by Ankur Singhal

Add json jarto your classpath

json jar添加到您的类路径

or use java -classpath json.jar ClassName

或使用 java -classpath json.jar ClassName

refer this

参考这个

回答by gprathour

The Exception it self says it all java.lang.ClassNotFoundException: org.json.JSONObject

异常本身说明了一切 java.lang.ClassNotFoundException: org.json.JSONObject

You have not added the necessary jar file which will be having org.json.JSONObjectclass to your classpath.

您尚未添加必要的 jar 文件,该文件将在org.json.JSONObject您的classpath.

You can Download it From Here

你可以从这里下载

回答by Raghu K Nair

Please add the following dependency http://mvnrepository.com/artifact/org.json/json/20080701

请添加以下依赖 http://mvnrepository.com/artifact/org.json/json/20080701

<dependency>
   <groupId>org.json</groupId>
   <artifactId>json</artifactId>
   <version>20080701</version>
</dependency>