如何在junit测试时解决NoClassDefFoundException?

时间:2018-01-16 11:22:01

标签: java junit

我想在我的Java程序中使用JUnit测试程序,但我得到了java.lang.NoClassDefFoundException。正如我所读到的,如果我没有.class文件,可能会得到。但是,首先我将它用于javac,因为我认为它必须在那里。

这是我的命令行:

myComputer\java\bead2>javac -cp .;junit-4.12.jar;hamcrest-core-1.3.jar
 snake\tests\ToApplesTest.java

myComputer\java\bead2>java  -cp .;junit-4.12.jar;hamcrest-core-1.3.jar
 org.junit.runner.JUnitCore snake\tests\ToApplesTest
JUnit version 4.12
Exception in thread "main" java.lang.NoClassDefFoundError: snake\tests\ToApplesTest (
wrong name: snake/tests/ToApplesTest)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)
        at org.junit.internal.Classes.getClass(Classes.java:16)
        at org.junit.runner.JUnitCommandLineParseResult.parseParameters(JUnitCommandL
ineParseResult.java:100)
        at org.junit.runner.JUnitCommandLineParseResult.parseArgs(JUnitCommandLinePar
seResult.java:50)
        at org.junit.runner.JUnitCommandLineParseResult.parse(JUnitCommandLineParseRe
sult.java:44)
        at org.junit.runner.JUnitCore.runMain(JUnitCore.java:72)
        at org.junit.runner.JUnitCore.main(JUnitCore.java:36)

为什么这会让我崩溃?

1 个答案:

答案 0 :(得分:0)

试试snake.tests.ToApplesTest。您必须提供包名称,而不是包含文件夹的路径。