在 Eclipse 中运行 junit 测试时出现 ClassNotFoundException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30443366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ClassNotFoundException when running a junit test in eclipse
提问by hudi
I am really desperate why this exception can even occurs ? I am running test in class MyTestIT
. And what class is not found ? Class which I run... I tried to clean and build it again in eclipse but with no success
我真的很绝望为什么这个异常甚至会发生?我正在课堂上进行测试 MyTestIT
。什么类没有找到?我运行的类......我试图在 Eclipse 中清理并再次构建它,但没有成功
Class not found it.mytest.MyTestIT
java.lang.ClassNotFoundException: it.mytest.MyTestIT
at java.net.URLClassLoader.run(URLClassLoader.java:366)
at java.net.URLClassLoader.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClass(RemoteTestRunner.java:685)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClasses(RemoteTestRunner.java:421)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
PS: I forgot to add it was working whole time. But today I just turn on eclipse and I cant run test here. With maven it is still working
PS:我忘了添加它一直在工作。但是今天我只是打开了eclipse,我不能在这里运行测试。使用 maven 它仍在工作
采纳答案by Peter Perhá?
Seems like some kind of weird eclipse issue, that will be difficult for us to diagnose, but the brute-force approach will be this: if you have maven on your project, just delete your project from your eclipse workspace (do not delete files), then from command line do:
看起来像是某种奇怪的 eclipse 问题,这对我们来说很难诊断,但蛮力方法是这样的:如果您的项目中有 maven,只需从您的 eclipse 工作区中删除您的项目(不要删除文件) ,然后从命令行执行:
mvn clean install eclipse:clean eclipse:eclipse
then re-import your existing project back into your eclipse workspace. should work fine after this procedure.
然后将现有项目重新导入到 eclipse 工作区中。在此程序后应该可以正常工作。
EDIT
编辑
Launch your test and switch to Debug perspective. You will see in the Debug view your most recent launch. Select it and go to its properties (shortcut: Alt+Enter). In the "command line" section you should see what was the exact command Eclipse used to launch your unit test. Check the classpath looks ok. It may be just some weird eclipse project setup. Maven will use a different classpath for running your tests, it may be that Eclipse is looking for your unit test in the wrong directory. If you class is in the classspath, then it must work.
启动您的测试并切换到调试透视图。您将在调试视图中看到您最近的启动。选择它并转到其属性(快捷键:Alt+Enter)。在“命令行”部分,您应该看到 Eclipse 用于启动单元测试的确切命令是什么。检查类路径是否正常。这可能只是一些奇怪的 eclipse 项目设置。Maven 将使用不同的类路径来运行您的测试,可能是 Eclipse 正在错误的目录中寻找您的单元测试。如果您的课程在类路径中,那么它必须工作。
回答by BPS
Delete the following files/folders from the Eclipse project:
从 Eclipse 项目中删除以下文件/文件夹:
.project .classpath .settings
.project .classpath .settings
Then re-import the project using "Import -> Existing Maven Projects".
然后使用“Import -> Existing Maven Projects”重新导入项目。
This should have the same effect as Peter's answer except no need to go to command line.
除了不需要转到命令行之外,这应该与彼得的答案具有相同的效果。
回答by Gopal
Check whether the project was build properly or not. you can check that in Problems tab (Windows -->Show View --> Problems). if you see "project can not build due to some error". Then first try to solve the error.
检查项目是否正确构建。您可以在问题选项卡(Windows --> 显示视图 --> 问题)中检查。如果您看到“由于某些错误,项目无法构建”。然后首先尝试解决错误。
Check your Project/bin folder , is it having your .class or not. if not then the project was not build properly.
检查您的 Project/bin 文件夹,是否有您的 .class 。如果不是,则项目未正确构建。
Do clean build. Make sure no build error for the project which has junit classes.
做干净的构建。确保具有 junit 类的项目没有构建错误。
回答by TheBitMuncher
This is a common problem where junit tests run successfully from maven but not from the eclipse junit test runner.
这是一个常见的问题,即 junit 测试从 maven 成功运行,但从 eclipse junit 测试运行器运行失败。
A solution is to manually add an external local copy of a relatively new junit jar to the top of the eclipse classpath in either your project properties build path or unit test runtime configuration. Ex:
一种解决方案是在您的项目属性构建路径或单元测试运行时配置中,将相对较新的 junit jar 的外部本地副本手动添加到 eclipse 类路径的顶部。前任:
- Click: Project - Properties - Java Build Path - Libraries - Add External JARs:
- Select a relatively new version of the junit jar locally on your machine
- Click the "Order and Export" tab - Move the junit jar to the top. (Apply and close)
- 点击:项目-属性-Java构建路径-库-添加外部JAR:
- 在您的机器上本地选择一个相对较新版本的 junit jar
- 单击“订购和导出”选项卡 - 将 junit jar 移至顶部。(申请并关闭)
You should now be able to right-click tests and run as junit test successfully.
您现在应该能够右键单击测试并成功运行为 junit 测试。
This was claimed to be fixed as an eclipse bug in 2017 "photon":
据称,这已在 2017 年“光子”中修复为日食错误:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=525844
https://bugs.eclipse.org/bugs/show_bug.cgi?id=525844
However there are dozens of posts in 2018/2019 that it's still occurring.
然而,在 2018/2019 年有几十个帖子仍在发生。
The fix above is a workaround hack - it's not a standard config setup that eclipse users should be expected to know.
上面的修复是一种解决方法 - 它不是 eclipse 用户应该知道的标准配置设置。
Some of the other workaround hacks didn't work for me:
其他一些解决方法对我不起作用:
- deleting meta files like .project/.classpath/.settings
- creating new eclipse workspaces
- running mvn eclipse goal targets
- running combinations of: multiple project-clean's, multiple mvn clean's, reopenging eclipse two times
- 删除元文件,如 .project/.classpath/.settings
- 创建新的 Eclipse 工作区
- 运行 mvn eclipse 目标目标
- 运行组合:多个 project-clean 的,多个 mvn clean 的,重新打开 eclipse 两次
回答by Abdull
I ran into this problem while working on a project with both Eclipse and mvn
-on-the-command-line concurrently.
我在同时使用 Eclipse 和mvn
-on-the-command-line处理一个项目时遇到了这个问题。
A command line-initiated mvn clean
had removed the ./target/
directory which Eclipse's JUnit support also relied upon to find compiled JUnit test classes.
命令行启动mvn clean
已删除./target/
目录,Eclipse 的 JUnit 支持也依赖该目录来查找已编译的 JUnit 测试类。
I resolved the Eclipse-based ClassNotFoundException
by running mvn test
from the command line first. From then on, Eclipse found the test class again.
我首先ClassNotFoundException
通过mvn test
从命令行运行来解决基于 Eclipse 的问题。从此,Eclipse 再次找到了测试类。
回答by Francisco Romero
Can you put here MyTestIt
class? If not, try to see if your code have one of this problems.
你能在这里MyTestIt
上课吗?如果没有,请尝试查看您的代码是否存在此类问题之一。
https://docs.oracle.com/javase/7/docs/api/java/lang/ClassNotFoundException.html
https://docs.oracle.com/javase/7/docs/api/java/lang/ClassNotFoundException.html