Code Pumpkin

Find from where java class is loaded | Class Loader SubSystem

November 1, 2017
Posted by Riteeka Rathod

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 Student.java 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){

        System.out.println(String.class.getClassLoader());
        System.out.println(Student.class.getClassLoader());
        System.out.println(School.class.getClassLoader());
    }
}

Hence School.class is on 2 paths viz.
1. Application Classpath
(system Properties -> Advanced -> Environment Variables -> Path-> C:ProgramFilesJavajdk1.8.0_101bin) and
2. Extension Classpath
(C:Program FilesJavajdk1.8.0_101jrelibext

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 Student.java
c:pumpkin_Classes> java Student
null
sun.misc.Launcher$AppClassLoader@1985a96
sun.misc.Launcher$ExtClassLoader@1085a85

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!

That's all for this topic. If you guys have any suggestions or queries, feel free to drop a comment. We would be happy to add that in our post. You can also contribute your articles by creating contributor account here.

Happy Learning 🙂

If you like the content on CodePumpkin and if you wish to do something for the community and the planet Earth, you can donate to our campaign for planting more trees at CodePumpkin Cauvery Calling Campaign.

We may not get time to plant a tree, but we can definitely donate ₹42 per Tree.



About the Author


I am made up of 57% Pizza, 3% water, 10% tea and 30% cheese. I wish to travel the whole world when I am in 50s .



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";
</code></pre>
For more information on supported HTML tags in disqus comment, click here.
Total Posts : 124
follow us in feedly

Like Us On Facebook