I hit the following problem recently on a project I was working on that used Eclipse and Maven. I’d checked out a bunch of projects from SVN into eclipse, and configured them as Maven projects. All appeared fine until I tried adding a Unit Test to one of the projects. Running the test as normal in Eclipse gave the following error:

Class not found com.cor.myproject.MyTest
java.lang.ClassNotFoundException: com.cor.myproject.MyTest
at java.net.URLClassLoader.findClass(URLClassLoader.java:434)
at java.lang.ClassLoader.loadClassHelper(ClassLoader.java:703)
at java.lang.ClassLoader.loadClass(ClassLoader.java:682)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:358)
at java.lang.ClassLoader.loadClass(ClassLoader.java:665)
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)

I did all the usual stuff – [Project .. Clean], [Maven .. Update Project], close and open – but nothing seemed to work. Checking online – these seemed to be the most common suggestions.

I noticed in the Project Properties there was no Java Builder – just the Maven Project Builder, Spring Project Builder and Validation Builders.

It is possible to add the Java Builder manually to the .project file. Just open the .project line and insert the following buildCommands and natures if they are not present:

<buildSpec>
    <buildCommand>
        <name>org.eclipse.jdt.core.javabuilder</name>
        <arguments>
        </arguments>
    </buildCommand>
</buildSpec>
<natures>
    <nature>org.eclipse.jdt.core.javanature</nature>
</natures>

I then closed and opened the project (not sure if this is entirely necessary) – reran the test, and this time it was fine.

(Visited 26,468 times, 1 visits today)
ClassNotFoundException Running JUnit Tests in Eclipse
Tagged on:     
InstagramPlease check your feed, the data was entered incorrectly.