2

I have followed this tutorial for using servlet for file upload.

Here is the folder structure (Dynamic project created using Eclipse). I am NOT using maven for this simple project. I added the external jar to the build path.

enter image description here

when I start Tomcat and deploy this application by right-clicking on the project in Eclipse > Run on Server I get

Caused by: java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory
    at java.lang.Class.getDeclaredFields0(Native Method)
    at java.lang.Class.privateGetDeclaredFields(Class.java:2499)
    at java.lang.Class.getDeclaredFields(Class.java:1811)
    at org.apache.catalina.util.Introspection.getDeclaredFields(Introspection.java:106)
    at org.apache.catalina.startup.WebAnnotationSet.loadFieldsAnnotation(WebAnnotationSet.java:270)
    at org.apache.catalina.startup.WebAnnotationSet.loadApplicationServletAnnotations(WebAnnotationSet.java:139)
    at org.apache.catalina.startup.WebAnnotationSet.loadApplicationAnnotations(WebAnnotationSet.java:65)
    at org.apache.catalina.startup.ContextConfig.applicationAnnotationsConfig(ContextConfig.java:415)
    at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:892)
    at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:386)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5380)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    ... 6 more
Caused by: java.lang.ClassNotFoundException: org.apache.commons.fileupload.FileItemFactory
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
    ... 20 more

please help me with what is going wrong here and how to fix this?

2
  • Did you made a Dynamic Web Project Version 3.0? You can check this info with : right click on project> Project Facets. Commented Feb 7, 2015 at 1:52
  • I do not see any Facets option. But I think it is 3.0 version from web.xml Commented Feb 7, 2015 at 1:56

1 Answer 1

8

You need to copy the required commons jars into your /WEB-INF/lib folder as well.

commons-io-2.4.jar
commons-fileupload-1.3.1.jar

The reason is that just adding the jar dependencies to your Eclipse project's build path does not make them automatically available on Tomcat. Since, the dependencies are missing (not found in the lib folder) your web app's deployment fails.

Sign up to request clarification or add additional context in comments.

6 Comments

this helped. but where are th class files (in this case UploadServlet.class) is stored. I looked into webapps folder but I do not see any context for UploadFileServlet(The project name as shown above)
May be you cant see the class files directly from Eclipse, But it is stored in folder TARGET/CLASS/. you can see the class file by exploring these folders(WORKSPACE/PROJECT/TARGET/CLASS)
@anptk: I want to know where tomcat keeps context folder that it serves. usually, .war files are put in webapps folder of tomcat. I do know where Eclipse puts the class files.
@anptk: I am able to run the servlet from right clicking on eclipse. But when I go into webapps folder, I do not see my project directory (uploadFileServlet) there which will serve as context root.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.