Manual setting CLASSPATH with -cp or -classpath does not work as expected
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.开发者_如何学PythonURLClassLoader.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?
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
精彩评论