Tuesday, November 8, 2011

ClassLoader (and CL Hell)

When we ran HelloWorld.class, we saw that the JRE looked at the CLASSPATH environment variable to know where to load the classes.

Then, when we ran HelloWorld.jar, we saw that the JRE looked at the Class-Path property in MANIFEST.MF

So what is going on? Why does the scenery change one way and another?!

Truth is that this scenery can keep changing in every environment!!
The culprit is called ClassLoader.

A ClassLoader is a special class which implements the ClassLoader abstract class.
You can write any number of ClassLoaders - MyFooClassLoader, MyYellowClassLoader, MyCrazyClassLoader, ...

JRE will call the ClassLoader to get a class it needs. Needless to say different ClassLoaders look in different places!!!

This leads to hair-tearing situations where Java programs deployed in one place work perfectly, and then stop working in another. Worse things can happen. If you happen to have two different versions of a class on your computer, one ClassLoader might pick one and another ClassLoader will pick the other. It will happen silently and your program can behave differently!!

This is ClassLoader HELL - it is an unsolved problem.

No comments: