Code Pumpkin

Find from where java class is loaded | Class Loader SubSystem

November 1, 2017
Posted by Pumpkin

In previous article, we discussed about 3 activites of Class Loader SubSystem i.e. LoadingLinkingInitialization and its Types of Class Loader  i.e. Bootstrap Class Loader or primordial Class LoaderExtension Class LoaderApplication Class Loader or System Class Loader. We have also seen how these Class loader uses Delegation hierarchy algorithm  to load Classes into memory area. In this article we'll discuss more about Class Loader and how to use Class Loader in program.

I would request you to go through previous articles before reading further. Let us understand loading of .class by Class Loader using delegation hierarchy algorithm by taking following example.

Create and School.javaand compile both of them.

Now, copy and paste School.class file  from Application Classpath to Extension Classpath.

public class Student{
    public static void main(String[] a){


Hence School.class is on 2 paths viz.
1. Application Classpath
(system Properties -> Advanced -> Environment Variables -> Path-> C:\ProgramFiles\Java\jdk1.8.0_101\bin) and
2. Extension Classpath
(C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext

whereasStudent.class is only on Application Classpath. Now question is which Class Loader will load which class into JVM,any guess?? check out the following output.

C:\pumpkin_Classes> javac
c:\pumpkin_Classes> java Student

From the above output we conclude that,

String.class is loaded by Bootstrap Class Loader from BootStrap Classpath. As Bootstrap Class Loader is not implemented in java, its object is not created and so Output is "null".

Bootstrap Class Loader is not java object hence we got null in the first case. But Extension and Application Class Loaders are java objects. Hence We are getting corresponding outputs for the remaining two System.out.println statements. which is [Classname@hashcodeInHexadecimalForm].

And as we have seen in  Delegation algorithm, Class Loader SubSystem will give the highest priority for bootstrap class path and then Extension Classpath followed by Application Classpath. School.class is loaded by Extension Class Loader first.

So in this article we discussed about how to Check ClassLoader of particular Class and it priority of loading into JVM. In next article, we will discuss about customized Class Loader. Stay tuned!

Liked this article? Take a second to support CodePumpkin on Patreon!

Tags: , , ,

Comments and Queries

If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code class="java"> 
String foo = "bar";
For more information on supported HTML tags in disqus comment, click here.
Contribute Your Articles

Interview Experiences

Subscribe Us

Like Us On Facebook

Support Code Pumpkin on Patreon!