我有以下文件夹结构
base directory = "."
build directory (for classes) = "./build/classes"
build directory (for test classes) = "./build/test/classes"
source = "./src"
test = "./test"
test_libraries = "./test_libraries"
当我使用命令
运行测试文件时,一切正常java -cp /usr/share/java/junit4-4.11.jar:build/classes:build/test/classes/ org.junit.runner.JUnitCore myclass.MyClassTest
但是当我将juint文件的位置更改为test_libraries/junit4-4.44.jar
时
然后我运行命令
java -cp ./test_libraries/junit4-4.11.jar:build/classes:build/test/classes/ org.junit.runner.JUnitCore myclass.MyClassTest
我收到以下错误
Exception in thread "main" java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
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.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 18 more
答案 0 :(得分:1)
您的类路径需要Hamcrest。
答案 1 :(得分:0)
你错过了Hamcrest Jar
http://search.maven.org/remotecontent?filepath=org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar
JUnit依赖于Hamcrest的很多方法。