【发布时间】: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