- 1 year ago 256
Java Question

ClassNotFoundException: org.apache.commons.cli.ParseException with maven

I am trying to run a java project from the command line in linux :

$ java -jar target/my-app.jar -csv test.csv

and got this error

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/cli/ParseException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(
at java.lang.Class.getMethod0(
at java.lang.Class.getMethod(
at sun.launcher.LauncherHelper.getMainMethod(
at sun.launcher.LauncherHelper.checkAndLoadMain(
Caused by: java.lang.ClassNotFoundException: org.apache.commons.cli.ParseException

I'm using maven-3, here my build maven configuration :


And my commons-cli dependency declaration

<!-- CLI -->

If I remove code & dependencies into my class I get no more error.

Thank you !

Answer Source

You are using maven but you are running the application from command line so you need to provide all the required jars to your application:

Approach 1: You can provide into your classpath like below:

$ java -jar -cp "list-of-jars" target/my-app.jar -csv test.csv

If you are on Windows the path will be semi colon separated and on Linux it will colon separated. You can use wild cards also like /*.jar to include all the jars(java6+).

Approach 2: You can use one fat/uber/one jar to combine all the jars into on jar run it like you want.

Below is using one-jar:

Using Maven: you need to update the plugins section pom.xml:


And update pluginRepositories section in pom.xml


When you will execute the mvn package you will get yourappname-one-jar.jar and you can run it java -jar yourappname-one-jar.jar

Approach 3: To use the maven shade plugin (as Robert suggested):

Add this into the plugins section of pom.xml:

                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">

Upon execution on mvn package the uber jar will be generated.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download