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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
brain storm
  • 30,124
  • 69
  • 225
  • 393

1 Answers1

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.

Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
  • 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) – brain storm Feb 07 '15 at 06:52
  • 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 Feb 07 '15 at 07:05
  • @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. – brain storm Feb 07 '15 at 07:08
  • This may help you, http://stackoverflow.com/questions/3405121/where-we-have-to-put-the-class-file-in-tomcat-directory – Anptk Feb 07 '15 at 07:19
  • @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. – brain storm Feb 07 '15 at 07:23
  • @brainstorm Open the "Server" view and doubleclick on your server. Check the server location and deploy path. Eg:- D:\workingfiles\spring_workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp2\wtpwebapps\StrutsLoginExample\WEB-INF\classes\com – Anptk Feb 07 '15 at 07:35