java.lang.NoClassDefFoundError:org / json / JSONObject [英] java.lang.NoClassDefFoundError: org/json/JSONObject

查看:98
本文介绍了java.lang.NoClassDefFoundError:org / json / JSONObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我的doPost()是:

My doPost() is:

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());}  
    }

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

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

当用Tomcat在Eclipse外部运行时,我得到错误:

When run outside Eclipse with Tomcat, I get error:

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)

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

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

JSONObject json = new JSONObject();

我已将org.json.jar添加到构建路径并将其添加到部署路径在属性 - >配置构建路径 - >部署程序集

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

推荐答案

否..这不是正确的方法。请参阅步骤,

对于类路径引用:右键单击Eclipse中的项目 - > Buildpath - > Configure Build path - > Java Build Path(左窗格) - > Libraries(Tab) - > Add External Jars - >选择你的jar并选择OK。

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.

对于部署程序集:右键单击eclipse-> Buildpath中的WAR - >配置构建路径 - >部署程序集(左窗格) - >添加 - >外部文件系统 - >添加 - >选择你的罐子 - >添加 - >完成。

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.

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

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

试试这个。当然它会奏效。尝试使用Maven,它将简化您的任务。

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

这篇关于java.lang.NoClassDefFoundError:org / json / JSONObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆