java.lang.NoClassDefFoundError and the bootstrap classloader

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 ;-)

WS-Trust presentation

These days I’ve been looking at WS-Trust. The starting point when dealing with WS-* is of course reading the right specification(s), but often examples are useful too to simply get a basic understanding of the tech while leaving the deep-in-details analisys for later in the learning process.

Well, I was googling on WS-Trust last week and found out a really nice video presentation on this topic and more generally on means of achieving message integrity and confidentiality using web services. I’m quite busy recently but I admit I looked at the whole presentation (almost 1h) even if I actually knew most of the contents… that remembered me my early approaches to webservices and message security at the university (perhaps that’s because of the italian accent of the speaker ;-) ). I think developers beginning with WS and security might found this a nice clip, good to get an idea of how low level cryptography is used as the building block for higher level specs like WS-Trust.

http://channel9.msdn.com/shows/Going+Deep/Vittorio-Bertocci-WS-Trust-Under-the-Hood/

P.S. despite being on a msdn host, there’s almost nothing specific to Microsoft there ;-)