59

I'm trying to run my junit test (to verify that a properties file loads correctly) but I get ClassNotFoundException although the class is there and all required libraries are there too.

Here it is the error I get :

Class not found ConfigurationManagerTest                                                 java.lang.ClassNotFoundException: ConfigurationManagerTest
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClass(RemoteTestRunner.java:693)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClasses(RemoteTestRunner.java:429)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

any ideas on how to fix this ?

Thanks.

7
  • 18
    try clean & build your project.
    – Harry Joy
    Commented Apr 19, 2011 at 12:27
  • 1
    tried it many times , i keep getting the same error Commented Apr 19, 2011 at 12:39
  • 3
    Open the Run Configuration window in eclipse, select the unit test you ran and go to tab classpath: here you can check what is actually in the classpath. Commented Apr 19, 2011 at 12:43
  • thanks, i saw an error above in the classpath tab : [JRE]: JRE not compatible with workspace .class file compatibility: 1.6 , maybe this the cause ? Commented Apr 19, 2011 at 12:53
  • I faced the same problem with a standalone Java class and a JUnit test, using Eclipse 4.2. I checked all paths etc. I tried "clean and build your project". It worked! Thanks Harry Joy. Commented Sep 18, 2012 at 11:29

27 Answers 27

41

ConfigurationManagerTest is not being found on your classpath. Ensure that the ConfigurationManagerTest.class file is available on your classpath.

It might not exist if it wasn't successfully compiled or if it's being created in a directory that you haven't told the Eclipse project should be on the classpath.

Assuming that you've put your test classes in a separate folder, make sure that it shows up in the "Package Explorer" view (rather than the "Navigator" view).

Under the project properties, "Java Build Path" and the "Source" tab, you can also see if the source folder is included for building as well as where the .class files are generated.

4
  • 8
    Adding some more on missing class files: Check if you see class files on the <my project>\build\classes\ dir. If you don't see any, check Eclipse's problems pane for errors about building your tests project. Fix those and it will build. If all else fails you might need to resort to Project->clean, then re built. And if you're really desperate restart Eclipse (A long shot). Commented Dec 15, 2011 at 15:31
  • 1
    I noticed in my eclipse project's setup that all of the build classes were not going into the default project build dir, so my tests couldn't even access the build artifacts.
    – bakoyaro
    Commented Nov 8, 2013 at 17:22
  • 1
    yes, my maven project was building .class files in /bin. had to add that dir under user entries in runconfig / classpath tab via advanced/add folder option.
    – theRiley
    Commented Feb 1, 2017 at 17:22
  • This worked for mo like a charm!! I have many (future) projects in my workspace that have compile issues. Adding the test class to the classpath ran the test that I needed.
    – Nash N
    Commented Mar 28, 2020 at 13:57
25

There is one more possibility. I had the same problem just now and no one of the solutions here helped. Except removing and recreating of the project - I didn't want to try it. What did help, was to clean the project two times immediately one after another! Clean + build could be repeated any number of times - it won't help. Only clean+clean and after that build goes OK. (Eclipse 3.6). Of course, you should disable autobuild for that.

Edit: This post has got its last plus on 15.11.2017. So, the problem (and the solution) remains actual.

14
  • Thanks, that helped, but I had to disable automatic build. Also I had to solve other missing libraries unrelated to jUnit. Commented Dec 22, 2013 at 22:21
  • Did not help at all (Eclipse 4.5) Commented Feb 2, 2016 at 10:09
  • 1
    @ChristianVielma I hope so. During 9 versions evolution they should remove the bug!
    – Gangnus
    Commented Feb 4, 2016 at 18:41
  • 1
    two times! that's Eclipse.
    – alexpfx
    Commented Apr 13, 2016 at 11:47
  • 1
    @Alessandro the magic is that they have the same bug for 4 years already. I am measuring the life length of the bug counting upvotes :-)
    – Gangnus
    Commented Oct 5, 2017 at 11:09
4

Another possible problem is a missing builder (it will prevent from your .class file from being built).

Check that your .project file has the following lines

<buildSpec>
  <buildCommand>
    <name>org.eclipse.jdt.core.javabuilder</name>
    <arguments>
    </arguments>
  </buildCommand>
</buildSpec>
<natures>
  <nature>org.eclipse.jdt.core.javanature</nature>
</natures>
1
  • This was my problem - forgot that my project was AspectJ enabled and so had the AJDT builder, which I didn't have installed. It's annoying that Eclipse will quietly refuse to build the project in these cases.
    – Nick H
    Commented Jan 18, 2013 at 16:08
3

This happened to me when I was dragging and dropping test classes to different packages. I just did the following

  1. Saved the class in a text editor.
  2. Deleted the offending class from eclipse project explorer.
  3. Re-created the class.

Bingo! I can now run the test!

2
  • Great! None of my tests was compiling anymore (no .class files). I deleted and recreated only one class using your method, and suddenly all test classes compiled again.
    – ROLO
    Commented Jul 14, 2015 at 14:09
  • Did not help at all in my case (Eclipse 4.5) Commented Feb 2, 2016 at 10:11
2

I had this problem and it was kind of tricky to realise what was wrong. The project had a dependency project with some error, which stopped the build from execute. When I remove this dependency problem, the project was built as expected.

Ps.: I am working on a project that has many compilation errors, because we are porting an application that was converted from Delphi to Java, so I didn't care to the compilation error at the beginning, that's why it took me some time to find out the problem.

2

check properties->java build path -> libraries. there should be no errors, in my case there was errors in the maven. once I put the required jar in the maven repo, it worked fine

2

I tried all the answers described here but none worked, but found this thread where slomek solves the problem in a very easy manner. Just go to project -> properties --> java build path. Then move Junit to the top by hitting the up bottom to the right. Then everything compiles just fine.

1

I tried everything mentioned here and in other posts. Some of the solutions that people proffered were:

  1. Change the output folder for the test
  2. Create a custom builder for the project that would run test-compile from Maven
  3. Move the Maven dependencies higher in the Order and Export list in the project build path

There were many, many more but the one that I found to work was as follows: Close the development environment. Delete the jars used by the project from my local Maven repository. Open the IDE. Build the project. Run the test.

After hours of beating my head against my keyboard and following suggested solutions, this one worked!

1

what worked for me is to remove the runconfiguration of the test. Then right click the testclass and click run as junit test.

now it recreates a correct run config for me.

1

Another way that this can unfold, and just did for me, is if you have a build error that Eclipse doesn't tell you about. If compiling the unit test fails then there is no .class file and you will get the ClassNotFoundException.

In my case there was a missing 3rd party jar file. When I ran the test I got a pop-up window that said "Errors exist in required project(s)". I work with a huge project and I always get that message because some of the source is not available to eclipse (long story). Also it doesn't say what the errors are. So I click "Continue" and then I get the exception.

Once I realized what was going on it was easy to fix by adding the missing jar to the classpath under Run -> Debug Configurations...

I'm not sure how to best detect or prevent this from happening except to be aware of this possibility when something goes wrong and to review your most recent changes for what might have gone wrong.

1
I also faced similar issue as mention in comment below.
    
    **org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClass(RemoteTestRunner.java:766)**
    
Check all mention possibility above but still could not find root cause why test case not getting execute.
Then I found that there is one package issue in my class. So due to that I was getting compilation error in my Test class. 
Reason is quite simple, Due to compilation error class did not compile and could not create class file. 
I fixed that compilation error and executed that Test class and its run successfully.
0

Are you sure your test class is in the build folder? You're invoking junit in a separate JVM (fork=true) so it's possible that working folder would change during that invocation and with build being relative, that may cause a problem.

Run ant from command line (not from Eclipse) with -verbose or -debug switch to see the detailed classpath / working dir junit is being invoked with and post the results back here if you're still can't resolve this issue.

0
0

A variation on Guy's answer above, involving additional builders. Say you have an Ant builder configured:

<buildSpec>
    <buildCommand>
        <name>org.eclipse.jdt.core.javabuilder</name>
        <arguments>
        </arguments>
    </buildCommand>
    <buildCommand>
        <name>org.eclipse.ui.externaltools.ExternalToolBuilder</name>
        <arguments>
            <dictionary>
                <key>LaunchConfigHandle</key>
                <value>&lt;project&gt;/.externalToolBuilders/myprojectantlaunch.launch</value>
            </dictionary>
        </arguments>
    </buildCommand>
</buildSpec>

If the Ant build and Eclipse build have differing output locations, Eclipse may not be able to locate the test class on the classpath. In the case of an Ant builder, also check the configured targets for clean, auto, manual and after-clean builds, to ensure that the target which builds the unit tests is called.

0

I add this answer as my solution review from the above.

  1. You simply edit the file .project in the main project folder. Use a proper XML Editor otherwise you will get a fatal error from Eclipse that stats you can not open this project.
  2. I made my project nature Javaby adding this <nature>org.eclipse.jdt.core.javanature</nature> to <natures></natures>.
  3. I then added those lines correctly indented <buildCommand><name>org.eclipse.jdt.core.javabuilder</name><arguments></arguments></buildCommand> to <buildSpec></buildSpec>.
  4. Run as JUnit ... Success
0

Right click the project in the Explorer: Build Path -> Order and Export -> Select JRE System Library [jdk] and click Bottom button.

0

Yet another variation.
Somehow, my formerly working test classes appeared to be running from some other location; my edits would not execute when I ran the tests.

I found that the output folder for my ${project_loc}src/test/java files was not what I expected. It had inadvertently been set to ${project_loc}target/classes. I set it properly in project properties, Java Build Path, Source tab.

0

If you have a maven project try to run:

mvn clean compile

and then in eclipse clean & build your project.

0

I had the similar problem with my Eclipse Helios which debugging Junits. My problem was little different as i was able to run Junits successfully but when i was getting ClassNotFoundException while debugging the same JUNITs.

I have tried all sort of different solutions available in Stackoverflow.com and forums elsewhere, but nothing seem to work. After banging my head with these issue for close to two days, finally i figured out the solution to it.

If none of the solutions seem to work, just delete the .metadata folder created in your workspace. This would create an additional overhead of importing the projects and all sorts of configuration you have done, but these will surely solve these issue.

Hope these helps.

0

In "Package Explorer" view, Right click your test class, then "Build Path">>"Include", it should be OK.

0

I too faced the same exception, none of the solutions over internet helped me out. my project contains multiple modules. My Junit code resides in Web module. And it's referring to client module's code.

Finally , I tried : Right click on (Web module) project -->build path--> source tab--> Link source --> added the src files location (Client module's)

Thats it! It worked like a charm Hope it helps

0

I received this error because I had recently created a new workspace and my "Installed JREs" were not set up correctly. Make sure in Preferences -> Java -> Installed JREs the root folder of your JDK is selected. The default for me in a new workspace was a JRE for some reason.

0

I know this has been already answered a long time ago but the answer didnt fix it for me but, I just removed everything from the sources in the buildpath settings and re-added them

0

I had this problem, in my case the problem relies in the compilation, I am using maven and test classes didn't compile.

I fixed it by doing a maven install (it compiles all the files), also you can check for other reasons, that avoid test to compile like if your "run configurations" is skipping the tests to save time.

0
  • Assumming you tried everything reasonable (delete target, refresh, clean, rebuild, maven install...) and that you realized that Eclipse does not behave reasonable...
  • Now: Check if there is no errors suggesting it can be a plugin (any plugin) that has something to do with tests.

  • In my case:

    • I removed EclEmma.
    • It started working...

Spent few hours on trying to guess it!.

0

Please make sure the output folder in Java Build Path tab set as like below,which would determine where the .class file are generated.Then clean the project.

enter image description here

0

The above mentioned solutions didn't work for me.

I just restarted my IDE by closing it and reopening it.

And then error disappeared and its working fine now.

0

You may need to do a combination of the above to resolve this problem. I normally get this error when I do

mvn clean install

and see a compile error preventing the my tests from fully compiling.

Assuming the issues resolved by the above have been dealt with, try clicking Project, Clean, and clean the project which contains the test you want to run. Also make sure Build Automatically is checked.

Not the answer you're looking for? Browse other questions tagged or ask your own question.