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.
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.
NoClassDefFoundError  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  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:
- Fix the build path problems
- Refresh the workspace
- Clean all projects
- If this did not help, restart the IDE, then retry steps 2 and 3.