Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUNIT Test class in Eclipse - java.lang.ClassNotFoundException

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.

like image 283
Mouna Cheikhna Avatar asked Apr 19 '11 12:04

Mouna Cheikhna


4 Answers

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.

like image 104
Alan Escreet Avatar answered Nov 16 '22 16:11

Alan Escreet


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.

like image 39
Gangnus Avatar answered Nov 16 '22 18:11

Gangnus


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>
like image 4
Guy Avatar answered Nov 16 '22 17:11

Guy


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!

like image 3
Robert Avatar answered Nov 16 '22 16:11

Robert