java.lang.NoClassDefFoundError and the bootstrap classloader

May 26, 2009 – 3:51 pm by Alessio SOLDANO
Share

Seasoned java developers know that the infamous java.lang.NoClassDefFoundError you can get at runtime might be due to a lot of different issues, the most trivial being libraries missing in classpath.

Of course tracking down the real problem might result quite more complex when multiple classloaders are involed. Things get even more subtle when the bootstrap classloader comes into play ;-)

I usually deal with a project requiring a couple of libraries to be installed to and loaded from a location specified when providing the virtual machine with the endorsed dir parameter (-Dendorsed.dirs=my_endorsed_dir). I’m not spending a lot of words on the reason for doing so, it should be enought to know I need to overwrite some classes already shipped with the JDK.

As you know the classes in libraries added to the endorsed dirs are loaded by the bootstrap classloader, before the system classloaders do their job with the other classes.

Yesterday I was adding a new API (interfaces only) to another module (ModuleB) of my project. After that I implemented the interfaces in my ModuleA, which builds to a jar that goes to endorsed dir. Compile phase all green, but NoClassDefFoundError at runtime.

Exception in thread “main” java.lang.NoClassDefFoundError: it/javalinux/blog/Foo
at java.lang.ClassLoader.findBootstrapClass(Native Method)
at java.lang.ClassLoader.findBootstrapClass0(ClassLoader.java:891)
at java.lang.ClassLoader.loadClass(ClassLoader.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:299)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)

The classpath is OK. The libraries contain all the classes, including the Foo class. What’s happening then?

Well, I definitely did a mistake, ie. I put the Foo class in ModuleA while it implements FooInterface from ModuleB. Nothing dangerous in that besides *only one* of them is loaded by the bootclassloader, hence the NoClassDefFoundError.The Foo class is indeed available, but it’s loaded by another classloader.

So, aways think about the way your project classes are loaded and… look carefully at the exception dump, even in the java.lang.Classloader package section ;-)

 
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • DZone
  • Reddit
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Slashdot
  • Technorati
  • YahooMyWeb
  • LinkedIn
  • StumbleUpon
  • TwitThis
  • Wikio

Post a Comment