java.lang.NoClassDefFoundError:org/apache/commons/

2019-08-09 16:31发布

问题:

I am trying to build a simple Spring MVC Web App with the functionality of file uploading.I got following error:

java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory

After a quick search,all answers pointed to the missing of dependencies,but it seems not to be true in my case:

I have included the following code in pom.xml:

<dependencies> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> </dependencies> with commons.io-2.4.jar and commons.fileupload-1.3.1.jar added into lib folder.

One interesting thing I found was that whenever I deleted the code:

   <bean id="multipartResolver"
      class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize">
        <value>10000000</value>
    </property>
    <property name="maxInMemorySize">
        <value>10000000</value>
    </property>
  </bean>

the web app works fine(of course I removed the form for file uploading as well. )

回答1:

If you visit the Maven Central Repository and enter the search term:

fc:org.apache.commons.fileupload.FileItemFactory

then every released artifact available containing that class will be listed.

You will find commons-upload 1.3.1 in that list.

Therefore you need to double check your deployment to ensure that jar is present.

Tip: Use fc: to locate jars in Maven Central that contain a specific class.



回答2:

try to update your jar version, for Example:

    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>2.0.0-SNAPSHOT</version>
    </dependency>

We cann't find versions upper 1.3 on Apache and mvnrepository.com , but you can try this : https://repository.jboss.org/commons-fileupload/commons-fileupload/2.0.0-SNAPSHOT/

In fact, I encountered the same problem and now working fine with version 2.0 under SpringMVC framework.