【问题标题】:java.lang.NoClassDefFoundError using org.json library [duplicate]java.lang.NoClassDefFoundError 使用 org.json 库 [重复]
【发布时间】:2016-02-02 11:48:39
【问题描述】:

我刚刚将org.json 库导入我的动态Web 项目。作为服务器,我使用的是Tomcat。当我尝试运行应用程序时,我收到此错误:java.lang.NoClassDefFoundError: org/json/JSONObject。这是我收到错误的代码:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pathInfo = request.getPathInfo();

        if(pathInfo != null){
            String newPathInfo = pathInfo.substring(1);
            System.out.println("--> " + pathInfo);
            System.out.println("--> " + newPathInfo);

            System.out.println("parameter --> " + request.getParameter("format"));
            String format = request.getParameter("format");

            if(format != null){
                if(format.equals("json")){                      
                    System.out.println("Preparing JSON reply...");
                    response.setContentType("text/json");

                    JSONObject obj = new JSONObject();
                    obj.put("salutation", lang.get(newPathInfo));

                    response.getWriter().write(obj.toString());
                    System.out.println("--> "+obj.toString());
                }   
            }
        }

具体来说,这是我收到错误的那一行:

obj.put("salutation", lang.get(newPathInfo));

我有什么遗漏吗?
谢谢!

【问题讨论】:

  • 听起来你缺少“在你的类路径中包含库”,但你还没有告诉我们你在做什么......
  • 您缺少 org.json jar 作为您项目的依赖项。您需要将其添加到您的类路径中。
  • 你的项目是用maven构建的吗?从您使用 WEB-INF/lib 的回答看来,它可能是......在这种情况下,您找到的解决方案掩盖了您在项目设置中遇到的一些其他问题。

标签: java json tomcat noclassdeffounderror


【解决方案1】:

我已将库复制到 WEB-INF/lib 文件夹,它正在工作

【讨论】:

    猜你喜欢
    • 2011-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-13
    • 2018-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多