0

I am trying to build with maven and run a jar that imports the apache commons fileupload library.

I have included in my pom.xml the below dependencies

<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.0</version>
</dependency>
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.5</version>
</dependency>

Running mvn clean package works with no errors.

But running the jar file with

java -cp .\target\XXX-server-1.0-SNAPSHOT.jar XXX.MainClass

produces the error

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory

I searched for a solution to this problem and the closest I found was java.lang.NoClassDefFoundError:org/apache/commons/fileupload/FileItemFactoryin Spring MVC, but am not sure where to find the "deployment" folder or if that is applicable to me as I am trying to run the jar in the target directory.

Do I need to move the actual fileupload library jar or some other file to get this to work? Thanks.

ansonl
  • 786
  • 1
  • 13
  • 34

2 Answers2

0

NoClassDefFoundError means it presents in compile time, but it is not found at runtime. Your java command doesn’t seem to include Apache Common FileUpload. When compile your project, file upload jar file should be downloaded to your local m2 repository and included to your project. Use the jar file and include it in classpath.

Hai Quach
  • 7
  • 5
0

How are you creating your jar? Through maven? You need to make sure that you include all the dependencies in the jar itself. Related question - How can I create an executable JAR with dependencies using Maven?

Alok Sinha
  • 171
  • 1
  • 3