Oct 7, 2017

Understanding JVM: How Class Loader works?

Previous article was related to the types of class loader. I this post, we will see how these types works to load a class file. 

Class loader sub system follows Delegation Hierarchy algorithm for loading any class file. It is explained below.

Step 1: When request comes for a class file, JVM first look if is already loaded or not.
Step 2: If already loaded, JVM finds it in the memory area.
Step 3: If not, it requests class loader sub system to load the requested class file.
Step 4: Class loader sub system first sends the request to Application class loader.
Step 5: Application class loader delegates the request to Extension class loader.
Step 6: Extension class loader delegates it further to Bootstrap class loader.
Step 7: Bootstrap class loader searches class file in the bootstrap class path. If found, it is loaded.
Step 8: If not found, Bootstrap class loader delegates request to Extension class loader.
Step 9: Extension class loader searches it in the extension class path. If found, it gets loaded.
Step 10: If not found, request is again delegated to the  Application class loader which searches class file in the application class path and loads if found.
Step 11: If still not found, ClassNotFoundExcetpion is thrown.

Thus, Bootstrap class loader has the highest priority while Application class loader has the lowest. If same name class file is present in more than one class paths, higher priority class loader will be given the preference.

In next article, we will talk about how can create a custom class loader and what is the need for it.
If you like reading our articles, do like and share with other peoples. You can also connect with us at +Java Territory or Java Territory to stay updated.

0 comments:

Post a Comment