简体   繁体   中英

Why java -cp cannot serach the .jar of directory?

I execute java -classpath .;C:\\libtest\\log4j-1.2.15.jar;C:\\devtool\\eclipse\\plugins\\org.junit_4.8.2.v4_8_2_v20110321-1705\\*.jar; org.junit.runner.JUnitCore com.xu.MyClass to execute all method in my junit test .

But it doesn't work.It report error below.

Exception in thread "main" java.lang.NoClassDefFoundError: org/junit/runner/JU
tCore
Caused by: java.lang.ClassNotFoundException: org.junit.runner.JUnitCore
        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:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: org.junit.runner.JUnitCore.  Program will exit.

But directory C:\\devtool\\eclipse\\plugins\\org.junit_4.8.2.v4_8_2_v20110321-1705 contains junit.jar which has this class. Why this happens?

If i execute java -classpath .;C:\\libtest\\log4j-1.2.15.jar;C:\\devtool\\eclipse\\plugins\\org.junit_4.8.2.v4_8_2_v20110321-1705\\junit.jar ; org.junit.runner.JUnitCore com.xu.MyClass.

It works.

I refer to the document from java command.

 -classpath <class search path of directories and zip/jar files>
               A ; separated list of directories, JAR archives,
               and ZIP archives to search for class files.

If you want to use the wildcard character (*), I think you should enclose your entry in " " in windows and ' ' in unix. Also remember the use of the wildcard character is only supported after Java 6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM