Welcome back NoClassDefFoundError in the Eclipse IDE

September 6, 2013 | 2 min Read

Situation 1: “Dad, where is my yellow road roller?” - “Let me see, son..” Looks around. “I have no idea. Are you sure you didn’t leave it at grandmas?”

Situation 2: “Dad, where is my green bulldozer?” - “Let me see, son..” Looks around. “Here it is, son … wait, where did it go? I was sure it just sat there in this box.” *Confusion*

Class loading is a peculiar thing, and every Java developer has seen exceptions around it. The ClassNotFoundException is well known to everybody who ever tried to load a class at runtime. Not so common is the NoClassDefFoundError, and I had not seen it for a while. Developing with the latest Eclipse release I see it more often and since it can be quite confusing, let me explain where it comes from and how to make it go away.

A ClassNotFoundException is something you get when you try to turn a String into a Class, say classloader.loadClass("MyRoadRoller"). All sorts of typos can happen, or the class is not on the classpath, and every seasoned Java developer knows how to address these sort of problems.

The NoClassDefFoundError [1] happens when a bunch of classes were compiled and linked together, and then one class is gone during a normal method call, i.e. grabToy(). If you want to provoke it in any Java system, just go and delete one specific class file from an otherwise sane classpath. Don’t recompile, but run your project.

So, the circumstances leading to a NoClassDefFoundError are relatively simple, and once you understand that it is different from the ClassNotFoundException the solution can also be relatively simple: Put the class back where it should be. The puzzling thing usually is to find out how the class vanished. Of course the general reasons may vary, and as a general solution [2] can be consulted. If you see it during normal development in the Eclipse IDE, here are some ideas how to get it away. I found it happening when my workspace did not completely recover from build path problems such as cycles in PDE projects:

  1. Fix the build path problems
  2. Refresh the workspace
  3. Clean all projects
  4. If this did not help, restart the IDE, then retry steps 2 and 3.

[1] https://docs.oracle.com/javase/7/docs/api/java/lang/NoClassDefFoundError.html

[2] https://javarevisited.blogspot.de/2011/06/noclassdeffounderror-exception-in.html

[3] https://blog.osgi.org/2011/05/what-you-should-know-about-class.html

Image via Creative Commons from Pintoys