使用org.json库时发生java.lang.NoClassDefFoundError
我刚刚将使用org.json库时发生java.lang.NoClassDefFoundError,java,json,tomcat,noclassdeffounderror,Java,Json,Tomcat,Noclassdeffounderror,我刚刚将org.json库导入到我的动态Web项目中。作为服务器,我使用的是Tomcat。当我尝试运行应用程序时,会出现以下错误:java.lang.NoClassDefFoundError:org/json/JSONObject。这是我得到错误的代码: protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
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));
具体来说,这是我得到错误的那一行:
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));
我有什么遗漏吗谢谢 我已将该库复制到
WEB-INF/lib
文件夹中,它正在工作听起来您缺少了“在类路径中包含库”,但您还没有告诉我们您正在做什么……您缺少org.json jar作为项目的依赖项。你需要将它添加到你的类路径中。你的项目是用maven构建的吗?从您使用WEB-INF/lib的回答来看,它可能是。。。在这种情况下,您找到的解决方案掩盖了您在项目设置中遇到的一些其他问题。