Java: NoClassDefFoundError using Apache Commons CLI 1.2

S.B picture S.B · May 30, 2012 · Viewed 8k times · Source

I am using the Apache Commons CLI 1.2 to parse command line arguments in Java. I had run into the NoClassDefFoundError when trying to run my java class but solved it by fixing the class-paths.

Now I have the same exception but in regards to the actual commons-cli classes as is shown below:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/cl i/CommandLineParser Caused by:
    java.lang.ClassNotFoundException: org.apache.commons.cli.CommandLineP arser
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source) Could not find the main class:  com.amirsys.score.api.script.CMDContentPusher.

The only thing I could think of is to set the class path to the commons-cli jar but that did not work. I haven't been able to find a solution to fixing the NoClassDefFoundError for imported classes. I thought these were compiled into the .class files?

Answer

Emmanuel Bourg picture Emmanuel Bourg · Apr 9, 2013

Commons CLI is not in the classpath at runtime. If you struggle to get the classpath right you can try to package your application as a single big jar file containing all its dependencies, including Commons CLI. There are many tools to achieve that (jarjar, onejar, Maven shade plugin...)