java 为什么我在指向 ServletFileUpload 的 HttpServletRequest 上收到 NoClassDefFoundError 错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4832779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why am I getting a NoClassDefFoundError on HttpServletRequest that is pointing to ServletFileUpload?
提问by katura
I recently started using JMeter to load test my webapp, locally on my pc. I have a jsp page for uploading images. The images are processed by my servlet. When I tried the process today, I got the following exception/error :
我最近开始使用 JMeter 在我的电脑上本地加载测试我的 web 应用程序。我有一个用于上传图片的jsp页面。图像由我的 servlet 处理。当我今天尝试该过程时,出现以下异常/错误:
exception
javax.servlet.ServletException: Servlet execution threw an exception
root cause
java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest
org.apache.commons.fileupload.servlet.ServletFileUpload.isMultipartContent(ServletFileUpload.java:68)
spyder.servlets.imageProcessing.ImageProcessingServlet.uploadEditedImagesToDB(ImageProcessingServlet.java:527)
spyder.servlets.imageProcessing.ImageProcessingServlet.doPost(ImageProcessingServlet.java:153)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
Here is the code from my servlet that the exception is referring to -
这是我的 servlet 中异常所指的代码 -
boolean isPart = ServletFileUpload.isMultipartContent(req);
....and yes, I've put all the necessary import
statements in the class.
....是的,我已经把所有必要的import
陈述都放在了课堂上。
I can't recall making any changes to anything on my system that would cause this problem. This process has always run without any problems, so I don't understand what is causing it to fail like this now. Its rather coincidental, I think, that it is failing after I've been using JMeter...
我不记得对我的系统上的任何会导致此问题的更改进行了任何更改。这个过程一直运行没有任何问题,所以我不明白是什么导致它现在像这样失败。我认为相当巧合的是,在我使用 JMeter 之后它失败了......
回答by BalusC
All 3rd party webapp libraries like Commons FileUpload belong in /WEB-INF/lib
of your webapp, not elsewhere. This exception can occur whenever you've placed it in JRE/lib
or JRE/lib/ext
.
所有 3rd 方网络应用程序库(如 Commons FileUpload)都属于/WEB-INF/lib
您的网络应用程序,而不是其他地方。只要您将其放入JRE/lib
或 中,就会发生此异常JRE/lib/ext
。
And indeed, as Bozho mentions, you need to ensure as well that you haven't moved/copied/duplicated any servletcontainer-specific libraries (which should be left untouched in Tomcat/lib
) around in different places of the classpath. But that should IMO not have resulted in this kind of exception. It's basically telling that the classloader which loaded the FileUpload API has totally no knowledge about the Servlet API.
事实上,正如 Bozho 所提到的,您还需要确保您没有在Tomcat/lib
类路径的不同位置移动/复制/复制任何特定于 servletcontainer 的库(应该在 中保持不变)。但是,如果 IMO 不会导致这种异常。这基本上说明加载 FileUpload API 的类加载器完全不了解 Servlet API。
If you read the Tomcat classloading HOW-TO, then you'll see that the libraries in JRE/lib
and JRE/lib/ext
are loaded by a different classloader (bootstrap) than the ones in Tomcat/lib
(common) and /WEB-INF/lib
(webapp). The bootstrap classloader has no knowledge about common and webapp libraries. It's the other way round. The commonclassloader has knowledge about the bootstrapclassloader and the webappclassloader has knowledge about both. Since the Servlet API is normally loaded by the commonclassloader, this can only mean that the FileUpload API was loaded by the bootstrapclassloader. And this is wrong :)
如果你读了Tomcat的类加载HOW-TO,那么你会看到,在图书馆JRE/lib
和JRE/lib/ext
由不同的类加载器(加载引导)比那些Tomcat/lib
(普通)和/WEB-INF/lib
(Web应用程序)。引导类加载器不了解 common 和 webapp 库。这是反过来的。在常见的类加载器有知识有关的引导类加载器和web应用程序的类加载器有大约两个知识。由于 Servlet API 通常由公共类加载器加载,因此这只能表示 FileUpload API 是由引导程序加载的类加载器。这是错误的:)
回答by Bozho
This means that your servlet container does not have the servlet api. Take a clean installation of Tomcat and try to deploy there. First check that you have the servlet api jar in tomcat/lib
. And make sure you don't have it in webapps/yourapp/WEB-INF/lib
这意味着您的 servlet 容器没有 servlet api。全新安装 Tomcat 并尝试在那里部署。首先检查您在tomcat/lib
. 并确保你没有它webapps/yourapp/WEB-INF/lib