Sunday, June 1, 2008

Classloaders in Java? How do they work?

Classloaders in Java

Classloaders are the Java program(s) i.e., Java classes that are responsible for loading classes into the underlying JVM. Any class before being used, should first be available and accessible to the classloader, which will load the class into the memory and then only the JVM can perform any task on it. Having said that we see that all the classes in Java are first loaded into the memory by a classloader (normally the default classloader) and then only the class can be used in any possible way. Right? We just said that classloaders are also Java classes only. Now, who loads this classloader classes then? Good pick, if you really picked it :-) The answer to this question is - the bootstrap classloader. Ohh... again a classloader. Yeah, it is, but different from what we discussed above. It's a native classloader and not written in Java. This bootstrap classloader is platform dependent (of course, that's why we called it native) and it's normally written in C.

How the Class-loading mechanism work in Java?

Whenever you try to execute a Java program by invoking the 'java' command then a native Java launcher program is first invoked. This launcher program is 'native' and hence platform dependent. Well... quite obvious, isn't it? Now, on a successful invokation of this launcher program, it calls the bootstrap classloader program. This bootstrap loader program then loads the Core Java classes (classes belonging to the packages starting with 'java.') and then loads two other classloaders - extension classloader and application classloader. That's it. The job of the bootstrap classloader is done once it completes these three tasks - One, loading Core Java classes; Two, loading extension classloader; Three, loading application classloader.

The bootstrap classloader transfer the control to the extension classloader, which then loads the extension classes into the memory. What are extension classes in Java? They are classes belonging to all the packages starting with 'javax.' OR the classes present in the 'ext' directory of the underlying JRE. You may like to go through the post Extension Mechanism in Java for more details.

Finally the control is transferred to the application classloader, which loads the classes of the current application.

One interesting point to note here is that all the three classloaders follow the delegation model i.e., if the application classloader requires to load a class, it'll first delegate a request for the same class to the extension classloader, which will in turn send a request to the bootstrap classloader for the same class. Now, the bootstrap classloader will check if that class is a Core Java class or not. If yes, then it'll make that available to the extension classloader in response to its request and finally the extension classloader will make that available to application classloader. If the bootstrap classloader discovers that the requested class if not a Core Java class, it notifies the same to the extension classloader. Now the extension classloader checks if this class is an extension class or not. If it discoveres that it's an extension class, it makes that available to the application classloader otherwise it notifies the application classloader that the requested class is not even an extension class. The application classloader itself loads a class in this case (when it has got notifications that the requested class is neither a Core Java class nor an extension class).



Manish said...

I had read abt Classloaders many times, could not understand fully even once before reading this article. So lucid, still so detailed. Very effective explanation. Really helpful.

Anonymous said...

Thanks for such a nice explanation. Keep up the good work.

Anonymous said...

Really good explanation. Keep up the good work.

Kesar said...

I agree with Manish, never understood the class loaders like this

Amila Nandana said...

Very good article for java class loading mechanism. Very nicely explain how delegation model works on class loader and lot of articles saying class loaders are given parent class loader to load classes. That concept explain very clearly. Nice

Anonymous said...

Best explanation about classloaders I ever read

Anonymous said...

amazing info, a tiny article with exact information i was looking for, thanks a lot, I don't remember if I had ever posted thanks for any information I was looking for. Thank you

Anonymous said...

I think the admin of this web site is truly working hard in support of his
web site, since here every information is quality based
Here is my webpage ; adsense tips

Garfield said...

Thanks for this article, truly helped me get started with the ClassLoader concept. Since I am moving from Native to Java, this was truly helpful.


Java tutorials said...

m 573Indeed great tutorial. Many people forget most basic thing about CL is location and sequence. Since every CL loads class from different location if same class present in multiple location, which one will be picked up and used is key thing to know. See How classloader works in Java for details

Java Tutorials said...

As far as the actual loading of classes to JVM memory is concerned, class loading involves multiple steps including the execution of static and instance blocks. Also any referring classes are loaded as and when encountered.