以下是为NetBean IDE创建的a Java program的目录结构:
$ ls
build.xml manifest.mf nbproject src test
package
中的src/steal/MMThread.java
语句是
package steal;
我按
编译.java文件$ javac src/steal/MMThread.java -d bin/steal/
$ ls bin/steal/
MMThread.class MMThread$Worker.class
然后我编译并运行了Java Junit测试程序。编译很好,但运行测试不是。那我该怎么办?感谢
$ javac -cp bin/:/home/t/program_files/programming/eclipse/plugins/org.junit_4.11.0.v201303080030/junit.jar test/steal/MMThreadTest.java
$ ls test/steal/
MMThreadTest.class MMThreadTest.java
$ java -cp bin/:/home/t/program_files/programming/eclipse/plugins/org.junit_4.11.0.v201303080030/junit.jar org.junit.runner.JUnitCore steal.MMThreadTest
JUnit version 4.11
Could not find class: steal.MMThreadTest
Exception in thread "main" java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:996)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:173)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:772)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:670)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:593)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:551)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:486)
at org.junit.runner.Computer.getSuite(Computer.java:28)
at org.junit.runner.Request.classes(Request.java:75)
at org.junit.runner.JUnitCore.run(JUnitCore.java:117)
at org.junit.runner.JUnitCore.runMain(JUnitCore.java:96)
at org.junit.runner.JUnitCore.runMainAndExit(JUnitCore.java:47)
at org.junit.runner.JUnitCore.main(JUnitCore.java:40)
Caused by: java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:553)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:486)
... 15 more
更新
$ java -cp test/:bin/:/home/t/program_files/programming/eclipse/plugins/org.junit_4.11.0.v201303080030/junit.jar org.junit.runner.JUnitCore steal.MMThreadTest
JUnit version 4.11
Exception in thread "main" java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:996)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:173)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:772)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:670)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:593)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:551)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:486)
at org.junit.runner.Computer.getSuite(Computer.java:28)
at org.junit.runner.Request.classes(Request.java:75)
at org.junit.runner.JUnitCore.run(JUnitCore.java:117)
at org.junit.runner.JUnitCore.runMain(JUnitCore.java:96)
at org.junit.runner.JUnitCore.runMainAndExit(JUnitCore.java:47)
at org.junit.runner.JUnitCore.main(JUnitCore.java:40)
Caused by: java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:553)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:486)
... 15 more
答案 0 :(得分:0)
问题不在于哈克雷斯特。
此代码使用Junit 3.8.2,您在类路径中提供Junit 4。 所以删除4并添加Junit 3.8.2
答案 1 :(得分:0)
类路径设置不正确。它需要:
.class
个文件 hamcrest的jar应该在你拥有junit的同一个目录中。
查看/home/t/program_files/programming/eclipse/plugins
。
您可以使用find
命令找到它:
find /home/t/program_files/programming/eclipse/plugins -name '*hamcrest*.jar'
如果您的机器没有hamcrest, 你可以从这里下载:
http://search.maven.org/remotecontent?filepath=org/hamcrest/hamcrest-all/1.3/hamcrest-all-1.3.jar
一旦你拥有了它,你可以把各个部分放在一起:
junit=/home/t/program_files/programming/eclipse/plugins/org.junit_4.11.0.v201303080030/junit.jar
hamcrest=/home/t/program_files/programming/eclipse/plugins/idontknowwhere/hamcrest.jar
java -cp "$junit:$hamcrest:test/steal" org.junit.runner.JUnitCore MMThreadTest