Sunday, May 20, 2012

loadClass - JDK 1.6.0_18 vs JDK 1.6.0_27

Recently while implementing a custom class loader, I hit upon a issue related to loadClass method of the Classloader.  The custom classloader code of interest is below.

This classloader worked on JDK 6 Update 27 (dev env) but failed to work on JDK 6 Update 17 (test env).

Its a rare skill to write code in Java that  fails between minor revisions!! :-)

The class loader code was written looking at the Java source code for Classloader. This was the cause of the issue - the custom loader implementation did not adhere to the documented contract for the loadClass method.

The documentation of the loadClass method clearly states

ClassNotFoundException - If the class could not be found

While the custom class loader method returns null if the class is not found! So why did it work in latest update of Java?  Following are the Classloader code snippets from the two JDK 6 updates that I have mentioned.

Jdk 1.6 Update 18

Jdk 1.6 Update 27

The if check c==null in the Update 27 made the loadClass method work even though it did not adhere to the documented contract.

The bottom line is to code against documented contract and not look at the implementation to write code!

No comments: