Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting a NoClassDefFoundError on HttpServletRequest that is pointing to ServletFileUpload?

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 :

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 -

boolean isPart = ServletFileUpload.isMultipartContent(req);

....and yes, I've put all the necessary import statements in the class.

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...

like image 804
katura Avatar asked Jan 28 '11 20:01

katura


2 Answers

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.

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.

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 common classloader has knowledge about the bootstrap classloader and the webapp classloader has knowledge about both. Since the Servlet API is normally loaded by the common classloader, this can only mean that the FileUpload API was loaded by the bootstrap classloader. And this is wrong :)

like image 158
BalusC Avatar answered Oct 24 '22 01:10

BalusC


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

like image 38
Bozho Avatar answered Oct 24 '22 00:10

Bozho