简体   繁体   English

在 Eclipse 中运行 junit 测试时出现 ClassNotFoundException

[英]ClassNotFoundException when running a junit test in eclipse

I am really desperate why this exception can even occurs ?我真的很绝望为什么这个异常甚至会发生? I am running test in class MyTestIT .我正在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我运行的类......我试图在 Eclipse 中清理并再次构建它,但没有成功

 Class not found it.mytest.MyTestIT
    java.lang.ClassNotFoundException: it.mytest.MyTestIT
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.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. PS:我忘了添加它一直在工作。 But today I just turn on eclipse and I cant run test here.但是今天我只是打开了eclipse,我不能在这里运行测试。 With maven it is still working使用 maven 它仍在工作

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.然后将您现有的项目重新导入您的 eclipse 工作区。 should work fine after this procedure.在此程序后应该可以正常工作。

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).选择它并转到其属性(快捷键:Alt+Enter)。 In the "command line" section you should see what was the exact command Eclipse used to launch your unit test.在“命令行”部分,您应该看到 Eclipse 用于启动单元测试的确切命令是什么。 Check the classpath looks ok.检查类路径是否正常。 It may be just some weird eclipse project setup.这可能只是一些奇怪的 eclipse 项目设置。 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. Maven 将使用不同的类路径来运行您的测试,这可能是 Eclipse 正在错误的目录中寻找您的单元测试。 If you class is in the classspath, then it must work.如果您的课程在类路径中,那么它必须工作。

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.除了不需要转到命令行之外,这应该与彼得的答案具有相同的效果。

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.我首先从命令行运行mvn test解决了基于 Eclipse 的ClassNotFoundException From then on, Eclipse found the test class again.从此,Eclipse 再次找到了测试类。

Check whether the project was build properly or not.检查项目是否正确构建。 you can check that in Problems tab (Windows -->Show View --> Problems).您可以在问题选项卡(Windows --> 显示视图 --> 问题)中检查。 if you see "project can not build due to some error".如果您看到“由于某些错误,项目无法构建”。 Then first try to solve the error.然后首先尝试解决错误。

Check your Project/bin folder , is it having your .class or not.检查您的 Project/bin 文件夹,是否有您的 .class 。 if not then the project was not build properly.如果不是,则项目未正确构建。

Do clean build.做干净的构建。 Make sure no build error for the project which has junit classes.确保具有 junit 类的项目没有构建错误。

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 .一种解决方案是在您的项目属性构建路径或单元测试运行时配置中,将相对较新的 junit jar 的外部本地副本手动添加到 eclipse 类路径的顶部 Ex:前任:

  • Click: Project - Properties - Java Build Path - Libraries - Add External JARs:点击:项目-属性-Java构建路径-库-添加外部JAR:
  • Select a relatively new version of the junit jar locally on your machine在您的机器上本地选择一个相对较新版本的 junit jar
  • Click the "Order and Export" tab - Move the junit jar to the top.单击“订购和导出”选项卡 - 将 junit jar 移至顶部。 (Apply and close) (申请并关闭)

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删除元文件,如 .project/.classpath/.settings
  • creating new eclipse workspaces创建新的 Eclipse 工作区
  • running mvn eclipse goal targets运行 mvn eclipse 目标目标
  • running combinations of: multiple project-clean's, multiple mvn clean's, reopenging eclipse two times运行组合:多个 project-clean 的,多个 mvn clean 的,重新打开 eclipse 两次

如果此问题仅发生在 Eclipse 中,则在所选项目上执行命令 Project -> Clean... 可能会有所帮助。

Can you put here MyTestIt class?你能把MyTestIt类放在这里吗? If not, try to see if your code have one of this problems.如果没有,请尝试查看您的代码是否存在此问题之一。

https://docs.oracle.com/javase/7/docs/api/java/lang/ClassNotFoundException.html https://docs.oracle.com/javase/7/docs/api/java/lang/ClassNotFoundException.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM