3

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?

wattostudios
  • 8,666
  • 13
  • 43
  • 57
S.B
  • 33
  • 1
  • 3
  • This might be useful: http://stackoverflow.com/questions/1457863/what-is-the-difference-between-noclassdeffounderror-and-classnotfoundexception – Jeremy Goodell May 30 '12 at 00:28
  • How exactly do you run the application? Looks like commons-cli is not on the classpath... – centic May 31 '12 at 19:26
  • I was trying to run it from the windows command line. So am I to add the class path to the commons-cli as well in my initial arguments? I am having a dreadful feeling that I am going to be lost in a yarn ball of class paths configurations... is there a simpler way? – S.B Jun 04 '12 at 22:06

1 Answers1

3

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...)

Emmanuel Bourg
  • 9,601
  • 3
  • 48
  • 76