Code Pumpkin

Find from where java class is loaded | Class Loader SubSystem

November 1, 2017
Posted by Pumpkin
Subscribe

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:\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 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!

If you guys have any suggestions or queries, feel free 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 🙂


Further Related Articles

  1. Types of Class Loader
  2. Class Loader SubSystem
  3. JVM Architecture Introduction
  4. How To Create Immutable Class In Java
  5. CountDownLatch Vs CyclicBarrier
  6. What is the use of private constructor in java?

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 Post : 80
Subscribe
Contribute Your Articles

Interview Experiences

Related Books

Like Us On Facebook

Alexa Page Rank