admin管理员组文章数量:1122846
And Hi,I have a java project with the following structure:
packagename
---src
------main (contains source for package classes)
------test (contains source for test classes)
---bin
------main
-------test
---lib (contains junit4 and hamcrest JAR files).
Each test file is NOT declared to be part of the package packagename. I am unable to run any test class (I have tried several of the test classes I have written) as any attempt to run them will throw 'java.lang.NoClassDefFoundError' with one of the 'user-defined' classes in the package.
I am compiling each test file (successfully) with the following command:
'/src/test $ javac -cp .:../../bin:../../lib/junit-4.13.2.jar -d ../../bin/test TestClassName.java'
And attempting to run a test class with the following command, taken from How to run JUnit test cases from the command line:
'/bin/test $ java -cp .:../main:../../lib/junit-4.13.2.jar org.junit.runner.JUnitCore TestClassName.'
This results in the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: cards/MyClass
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:578)
at java.base/java.lang.Class.forName(Class.java:557)
at org.junit.internal.Classes.getClass(Classes.java:42)
at org.junit.internal.Classes.getClass(Classes.java:27)
at org.junit.runner.JUnitCommandLineParseResult.parseParameters(JUnitCommandLineParseResult.java:98)
at org.junit.runner.JUnitCommandLineParseResult.parseArgs(JUnitCommandLineParseResult.java:50)
at org.junit.runner.JUnitCommandLineParseResult.parse(JUnitCommandLineParseResult.java:44)
at org.junit.runner.JUnitCore.runMain(JUnitCore.java:72)
at org.junit.runner.JUnitCore.main(JUnitCore.java:36)
Caused by: java.lang.ClassNotFoundException: cards.EmptyDeckException
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:528)
... 10 more
I am not using an IDE nor a build system like maven. I have experimented with modifying the classpath when running junit (for example including all source files) but the same result occurs. My understanding is that if the classpath contains the binary files for the user-defined classes then junit should be able to find them when running tests. Any help would be greatly appreciated.
And Hi,I have a java project with the following structure:
packagename
---src
------main (contains source for package classes)
------test (contains source for test classes)
---bin
------main
-------test
---lib (contains junit4 and hamcrest JAR files).
Each test file is NOT declared to be part of the package packagename. I am unable to run any test class (I have tried several of the test classes I have written) as any attempt to run them will throw 'java.lang.NoClassDefFoundError' with one of the 'user-defined' classes in the package.
I am compiling each test file (successfully) with the following command:
'/src/test $ javac -cp .:../../bin:../../lib/junit-4.13.2.jar -d ../../bin/test TestClassName.java'
And attempting to run a test class with the following command, taken from How to run JUnit test cases from the command line:
'/bin/test $ java -cp .:../main:../../lib/junit-4.13.2.jar org.junit.runner.JUnitCore TestClassName.'
This results in the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: cards/MyClass
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:578)
at java.base/java.lang.Class.forName(Class.java:557)
at org.junit.internal.Classes.getClass(Classes.java:42)
at org.junit.internal.Classes.getClass(Classes.java:27)
at org.junit.runner.JUnitCommandLineParseResult.parseParameters(JUnitCommandLineParseResult.java:98)
at org.junit.runner.JUnitCommandLineParseResult.parseArgs(JUnitCommandLineParseResult.java:50)
at org.junit.runner.JUnitCommandLineParseResult.parse(JUnitCommandLineParseResult.java:44)
at org.junit.runner.JUnitCore.runMain(JUnitCore.java:72)
at org.junit.runner.JUnitCore.main(JUnitCore.java:36)
Caused by: java.lang.ClassNotFoundException: cards.EmptyDeckException
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:528)
... 10 more
I am not using an IDE nor a build system like maven. I have experimented with modifying the classpath when running junit (for example including all source files) but the same result occurs. My understanding is that if the classpath contains the binary files for the user-defined classes then junit should be able to find them when running tests. Any help would be greatly appreciated.
Share Improve this question edited Nov 21, 2024 at 13:26 Marce Puente 3192 silver badges8 bronze badges asked Nov 21, 2024 at 12:20 everything is fineeverything is fine 11 bronze badge1 Answer
Reset to default -1You need to add the location of the class you're trying to test (and all its dependencies) to the classpath...
本文标签: junit 4 tests throwing javalangNoClassDefFoundErrorStack Overflow
版权声明:本文标题:junit 4 tests throwing java.lang.NoClassDefFoundError - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736310956a1934565.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论