java.lang.NoClassDefFoundError:org/json/JSONObject

Hit*_*ker 23 java json servlets

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

我的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());}  
    }
Run Code Online (Sandbox Code Playgroud)

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

当使用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)
Run Code Online (Sandbox Code Playgroud)

Server1.doPost(Server1.java:25)行引用

JSONObject json = new JSONObject();
Run Code Online (Sandbox Code Playgroud)

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

Jay*_*ram 29

不..这不是正确的方式.参考步骤,

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

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

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

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


Rag*_*air 23

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

<dependency>
   <groupId>org.json</groupId>
   <artifactId>json</artifactId>
   <version>20080701</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

98925 次

最近记录:

9 年,9 月 前