views:

152

answers:

1

MyClassWithMainMethod.java uses classes of someJar.jar.

If I call:

java -cp someJar.jar MyClassWithMainMethod

I get the exception:

Exception in thread "main" java.lang.NoClassDefFoundError: MyClassWithMainMethod
Caused by: java.lang.ClassNotFoundException: MyClassWithMainMethod
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:315)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
at java.lang.ClassLoader.loadClass(ClassLoader.java:250)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:398)

But when I set the CLASSPATH to my jar manually

export CLASSPATH=:/path/to/someJar.jar

it works calling

java MyClassWithMainMethod

What am I doing wrong?

+6  A: 

What about

java -cp /path/to/someJar.jar MyClassWithMainMethod

If you don't give Java the complete path to the jar file, how do you expect that it would find it?

OK well the argument you give to "-cp" is the same sort of thing you use with the CLASSPATH variable - what happens when you do this:

java -cp .:someJar.jar MyClassWithMainMethod
Pointy
The error shows that it's his class which isn't found, not one in the JAR
Valentin Rocher
Both someJar.jar file and MyClassWithMainMethod are in the same directory from which I call java.
Sney
OK I updated the answer
Pointy
@Pointy: Great, that works :) Could you tell me what the colon means?
Sney
Colon is the separator between different jars when running on Unix/Linux/OSX. For windows it's semi-colon
Kennet
Yes, but it's the one and only jar file I use. So why do I have to use it?
Sney
You have to tell Java that it should search for classes in **two** places: one, the .jar file, and the other, your current directory. If you were to add your class to the jar file, then the jar file would be the only thing you would need in the classpath.
Pointy