ClassNotFoundException when running junit test in eclipse
I am really desperate, why could this exception even happen? I am doing a test in class MyTestIT
. Which class was not found? The class I am running ... I tried to clean up and build it again in eclipse but with no success
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 that it worked all the time. But today I just turn on eclipse and I cannot run the test here. With maven it still workssource to share
Sounds like some weird eclipse problem that will be difficult for us to diagnose, but the brute force approach would be this: if you have maven in your project, just delete the project from the eclipse workspace (don't delete files) and then from the command line do :
mvn clean install eclipse:clean eclipse:eclipse
then re-import the existing project back into the eclipse workspace. should work fine after this procedure.
EDIT
Run the test and switch to the debug perspective. You will see your last run in the Debug view. Select it and go to its properties (shortcut: Alt + Enter). In the Command Line section, you should see which command Eclipse used to run your unit test. Check that the classpath looks ok. It might just be some weird eclipse project setup. Maven will use a different classpath to run your tests, perhaps Eclipse is looking for your unit test in the wrong directory. If your class is on the classpath it should work.
source to share
Can you put a class here MyTestIt
? If not, try to find out if your code has one of these problems.
https://docs.oracle.com/javase/7/docs/api/java/lang/ClassNotFoundException.html
source to share
I ran into this issue while collaborating on a project with Eclipse and mvn
-on-the-line-line.
The command line mvn clean
removed the directory ./target/
where Eclipse JUnit support was also used to find compiled JUnit test classes.
I resolved ClassNotFoundException
based on Eclipse by running mvn test
from command line. Eclipse has since discovered the test class again.
source to share