Java NoClassDefFoundError for commons-io?

82 views Asked by At

I have the following directory structure:

somedir/
    lib/
        myapp.jar
        commons-io-2.1.jar
        ...lots of otherjars

From inside somedir, I try to run the following command:

java -cp lib/* net.myapp.Driver /home/myUser.blah.text

And I am getting the following exception:

Exception in thread "main" java.lang.NoClassDefFoundError: lib/commons-io-2/1/jar
Caused by: java.lang.ClassNotFoundException: lib.commons-io-2.1.jar
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: lib/commons-io-2.1.jar.  Program will exit.

Why is it telling me that it can't find a "main" in Apache Commons IO? I might be able to understand if it couldn't find my Driver class (maybe I misconfigured something), but this is just throwing me way off. Ideas? Thanks in advance!

3

There are 3 answers

0
Abdul Salam On

you could also try java -cp /lib/* net.myapp.Driver /home/myUser.blah.text

0
4J41 On

Use quotes.

java -cp "lib/*" net.myapp.Driver /home/myUser.blah.text

Well explained here.

0
jpvee On

From the stack trace it appears as if java is taking the commons-io Jar for a class which leads me to a wild guess: Could it be a whitespace problem? Is it possible that lib contains a jar with a space in it? Have you tried enclosing the cp option in quotes (i.e. use -cp "lib/*")?