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

biology.info picture biology.info · Sep 1, 2015 · Viewed 16.3k times · Source

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(Class.java:2531)
    at java.lang.Class.getMethod0(Class.java:2774)
    at java.lang.Class.getMethod(Class.java:1663)
    at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.cli.ParseException

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

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                    <sourceEncoding>${project.build.sourceEncoding}</sourceEncoding>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>${appClass}</mainClass>
                            <classpathPrefix>lib/</classpathPrefix>
                            <useUniqueVersions>false</useUniqueVersions>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

And my commons-cli dependency declaration

        <!-- CLI -->
        <dependency>
            <groupId>commons-cli</groupId>
            <artifactId>commons-cli</artifactId>
            <version>1.2</version>
        </dependency>

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

Thank you !

Answer

Garry picture Garry · Sep 1, 2015

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:

<plugin>
        <groupId>org.dstovall</groupId>
        <artifactId>onejar-maven-plugin</artifactId>
        <version>1.4.4</version>
        <executions>
          <execution>
            <goals>
                <goal>one-jar</goal>
            </goals>
          </execution>
        </executions>
    </plugin>

And update pluginRepositories section in pom.xml

<pluginRepository>
        <id>onejar-maven-plugin.googlecode.com</id>
        <url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
</pluginRepository>

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:

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.4.1</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>org.sonatype.haven.HavenCli</mainClass>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>

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