6

I am trying to run JUnit tests from Eclipse but while running these tests, I am getting ClassNotFound Exception

Complete Stack Trace follows:

java.lang.NoClassDefFoundError: of Caused by: java.lang.ClassNotFoundException: of at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:248) Exception in thread "main"

I have searched google for this and have found that couple of guys have faced this issue but none of them seems to be solving my issue. This can probably be due to difference in platforms

My development platform is as follows

OS: Windows XP
Eclipse JUNO
JUnit4
Maven2

Mean while, If I run this project using maven it runs absolutely fine.

The most strange part for me is the name of the class for which ClassNotFound Exception is thrown. Name of the class is of.

This is what blows me completely.

Also, what ever stack trace I have listed above, that is all what I get when I get the error nothing else.

I have been stuck in this issue for 2 days now, any help would be greatly appreciated.

j0k
  • 22,600
  • 28
  • 79
  • 90
Bagira
  • 2,149
  • 4
  • 26
  • 55
  • 2
    Does doing a Project->Clean on your project in Eclipse solve the issue? – Shivan Dragon Sep 10 '12 at 13:08
  • What kind of class is this? of?? – Prasad S Deshpande Sep 10 '12 at 13:16
  • ".ClassNotFoundException: ?? of ?? at ??" is NOT a class name, it just fails to report the error details correctly. Maven usually spends some time setting up a project before running anything - check what it adds to project classpath that you don't already have in eclipse classpath. If **you** are not using dynamic class loader in your main method I would suspect that it is JUnit class that it can not find. – Germann Arlington Sep 10 '12 at 13:29
  • @ShivanDragon No, i have tried it already and it didn't help. – Bagira Sep 11 '12 at 05:01
  • @PrasadSDeshpande I have no idea, this is why i have written Name of the class blows me completely. – Bagira Sep 11 '12 at 05:02
  • @GermannArlington I have checked for JUnit jar is in the classpath. – Bagira Sep 11 '12 at 07:30
  • Till now no one is able to resolve your issue, can we move ahead if you please share snippet of pom.xml and your `JUnit` code snippet as wel. – Prasad S Deshpande Sep 12 '12 at 18:09
  • The below suggestion worked for me. http://stackoverflow.com/questions/11381068/class-not-found-exception-when-running-junit-test/17768624#17768624 – Paramesh Korrakuti Jul 22 '15 at 10:28

9 Answers9

12

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! If that doesn't work, you can try searching for the solutions I listed above.

gffny
  • 436
  • 5
  • 8
  • Suddenly tests started to work for me 4-5 days after posting the problem but i never realized, it was due to the these steps which i followed for some other reasons, it started to work. – Bagira May 18 '13 at 01:53
  • Had the problem in environments where I do not consistently use the maven plugin in Eclipse : having projects imported as Maven projects in Eclipse but doing `mvn eclipse:clean` and `mvn eclipse:eclipse` in terminals at the same time. Deleting the projects in eclipse and reimporting it worked for me without deleting any jars in the local maven repo. – Yves Nicolas Sep 18 '15 at 06:18
7

Right clicking on the project name in Eclipse and choosing "Maven --> update project" will often solve this problem.

user3821026
  • 81
  • 1
  • 1
  • 1
    Not sure why this was downvoted, as this worked for me, so I shall upvote it. In the "Update Maven Project" dialog, I also ensured that "Force Update of Snapshots / Release" was ticked as was "Clean Projects". Once the Maven update completed, my tests ran. Also note: I am using SpringSource ToolSuite version 3.6.2 – Phil Oct 24 '14 at 09:09
1

I know this question has already been answered but I will still share my solution here,

  1. Make sure the output folder of 'src/test/java' has been set to 'target/test-classes', this has been said in many other posts so I will not repeat here.

  2. If it still does not work, it is very likely that there are errors during the compiling of your unit test classes. Go 'Window' -> open 'Error Log' view, you should be able to see the detail of the error message.

For my case, a previously downloaded dependency jar is somehow corrupted due to network problems, so just delete the jar from local repository and use 'maven' -> 'update configuration' to download it again.

Then everything works!

rafoul
  • 11
  • 1
1

Normally when we build we check "Skip Tests" in "Run configuration tab", instead uncheck "Skip Tests" and build the project the junits should work fine. This solution worked for me. Run configuration tab for building project

saikiran_hegde
  • 746
  • 7
  • 11
0

try switching the perspective to JUnit in eclipse and run the tests again

0

Despite all the answers and other posts I saw. I will give a summary of my effort:

it is probably this project's configuration doesn't fit with other projects in the same workspace. ( how to solve this is probably another question )

so, close this workspace, start a clean workspace for this project alone.

zinking
  • 5,561
  • 5
  • 49
  • 81
0

Was facing the same issue. This simple step solved it for me.

  1. Remove JUnit from the project's Build Path and Add it again.

  2. If set to Build Automatically, your workspace would be built again. If not set to Build Automatically then build your Project. Your issue should be solved.

Arthas
  • 421
  • 1
  • 6
  • 23
0

You should add classes you use for example (spring-boot):

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {DataPoint.class})
public class DataPointTest {

    @Test
    public void TestBuilder() {
        DataPoint.Builder builder = new DataPoint.Builder();

        DataPoint dataPoint = builder.withCount(4).withFirst(1).build();

        assert dataPoint.getCount() == 4 && dataPoint.getFirst() == 1;
    }

}
Tamer Awad
  • 392
  • 7
  • 10
0

You can also check the info in POM.xml

I had the same issue. My project name was mismatching while setup the code.

<groupId>CucumberTest</groupId>
<artifactId>com.ProjectName</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>com.ProjectName</name>
<url>http://maven.apache.org</url>

After changing the project name in POM.xml it was working fine.