ClassNotFoundException when running a junit test in eclipse
Asked Answered
T

7

5

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

 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. But today I just turn on eclipse and I cant run test here. With maven it is still working

Tablecloth answered 25/5, 2015 at 17:35 Comment(0)
Z
7

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:

mvn clean install eclipse:clean eclipse:eclipse

then re-import your existing project back into your eclipse workspace. 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). 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.

Zagreb answered 25/5, 2015 at 17:49 Comment(3)
hm thx this should work but I cant really understand how this error even occurs and how should I avoid it. My project is really huge and importing all necessary projects take over a couple of timeTablecloth
You can also do this from within Eclipse. I'll post a separate answer.Karykaryl
thx, I used the above command and then go to project>build all, and it works!Octaviooctavius
S
1

I ran into this problem while working on a project with both Eclipse and mvn-on-the-command-line concurrently.

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.

I resolved the Eclipse-based ClassNotFoundException by running mvn test from the command line first. From then on, Eclipse found the test class again.

Spermine answered 21/10, 2016 at 10:20 Comment(0)
K
1

Delete the following files/folders from the Eclipse project:

.project .classpath .settings

Then re-import the project using "Import -> Existing Maven Projects".

This should have the same effect as Peter's answer except no need to go to command line.

Karykaryl answered 25/10, 2016 at 14:3 Comment(0)
J
0

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.

Check your Project/bin folder , is it having your .class or not. if not then the project was not build properly.

Do clean build. Make sure no build error for the project which has junit classes.

Jerri answered 18/7, 2018 at 13:9 Comment(0)
T
0

This is a common problem where junit tests run successfully from maven but not from the eclipse junit test runner.

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:

  • 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)

You should now be able to right-click tests and run as junit test successfully.

This was claimed to be fixed as an eclipse bug in 2017 "photon":

https://bugs.eclipse.org/bugs/show_bug.cgi?id=525844

junit error

However there are dozens of posts in 2018/2019 that it's still occurring.

The fix above is a workaround hack - it's not a standard config setup that eclipse users should be expected to know.

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
Tantalate answered 30/1, 2020 at 16:33 Comment(0)
W
0

If this problem occurs in Eclipse only, executing command Project -> Clean... on selected project may help.

Wil answered 19/3, 2021 at 17:43 Comment(0)
B
-1

Can you put here MyTestIt class? 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

Bi answered 25/5, 2015 at 17:41 Comment(1)
with maven test are still working. Just in eclipse there is problemTablecloth

© 2022 - 2024 — McMap. All rights reserved.