Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flow of class loading for a simple program

I am just now beginning to learn the internal architecture of Java. I have roughly understood the concept of class loading which loads the required classes when jvm runs, ClassNotFoundException is thrown when a class is not found and specific class loader loads the classes referenced by the class.

Can someone please explain clearly the flow of class loading i.e. the sequence of bootstrap class loading and user-defined class loading in the sample Java code below.

import java.io.File; public class Sample {     public static void main(String[] args)     {         String fileName = "sample";         File file = new File(fileName);         file.isFile();     } }  

Also I learnt from a reference material that "classloader maintains the namespaces of the classes it loads". By namespaces, does that mean the literal names of the class? Also can someone please explain the implication/advantage of that?

like image 896
Aarish Ramesh Avatar asked Dec 27 '13 06:12

Aarish Ramesh


People also ask

How does class loading work?

A Java Class is stored in the form of byte code in a . class file after it is compiled. The ClassLoader loads the class of the Java program into memory when it is required. The ClassLoader is hierarchical and so if there is a request to load a class, it is delegated to the parent class loader.

What is the order of class loading in Java?

Note: The ClassLoader Delegation Hierarchy Model always functions in the order Application ClassLoader->Extension ClassLoader->Bootstrap ClassLoader. The Bootstrap ClassLoader is always given the higher priority, next is Extension ClassLoader and then Application ClassLoader.

How do you load a class?

Load class with forName() method in Java The class object associated with the class with the given string name can be returned with the method java. lang. Class. forName(String name, boolean initialize, ClassLoader loader), using the class loader that is used to load the class.

What is class loader and how .class will be loaded?

ClassLoader is hierarchical in loading a class into memory. Whenever a request is raised to load a class, it delegates it to the parent classloader. This is how uniqueness is maintained in the runtime environment. If the parent class loader doesn't find the class then the class loader itself tries to load the class.


2 Answers

You will run your Sample class as follows

> java Sample

for little magic, check out the output of-verbose:class option and you see tons of following lines..

[Opened C:\jdk1.6.0_14\jre\lib\rt.jar] [Loaded java.lang.Object from C:\jdk1.6.0_14\jre\lib\rt.jar] [Loaded java.io.Serializable from C:\jdk1.6.0_14\jre\lib\rt.jar] [Loaded java.lang.Comparable from C:\jdk1.6.0_14\jre\lib\rt.jar] . . . . . . [Loaded java.security.cert.Certificate from C:\jdk1.6.0_14\jre\lib\rt.jar] [Loaded Sample from file:/D:/tmp/] [Loaded java.lang.Shutdown from C:\jdk1.6.0_14\jre\lib\rt.jar] [Loaded java.lang.Shutdown$Lock from C:\jdk1.6.0_14\jre\lib\rt.jar] 

You see a bunch of classes from \jre\lib\rt.jar are loaded, much before your class is loaded by Bootstrap class loader (or Primordial ). These are the pre-requisite for running any Java program hence loaded by Bootstrap.

Another set of jars is loaded by Extension class loader. In this particular example, there was no need of any classes from the lib \jre\lib\ext hence its not loaded. But Extension class loader are specifically assigned the task of loading the classes from the extension lib.

EDIT: Apart from the standard platform java classes Sun/Oracle also provide a set of jars which are used to extend the platform's core API. The jars placed in the extension lib folder are automatically placed in the classpath and hence not needed to be included in classpath explicitly. Here is nice official article on the same topic.

Finally, your class Sample is loaded by Application class loader after Bootstrap and Extension have finished loading.

like image 69
Santosh Avatar answered Sep 20 '22 19:09

Santosh


Classloader hierarchy

Whenever a new JVM is started the bootstrap classloader is responsible to load key Java classes (from java.lang package) and other runtime classes to the memory first. The bootstrap classloader is a parent of all other classloaders. Consequently, it is the only one without a parent.

Next comes the extension classloader. It has the bootstrap classloader as parent and is responsible for loading classes from all .jar files kept in the java.ext.dirs path–these are available regardless of the JVM’s classpath.

The third and most important classloader from a developer’s perspective is the system classpath classloader, which is an immediate child of the extension classloader. It loads classes from directories and jar files specified by the CLASSPATH environment variable, java.class.path system property or -classpath command line option.

Classloader hierarchy

ClassLoader Namespace

In Java a class is uniquely identified using ClassLoader + Class as the same class may be loaded by two different class loaders.

Class A loaded by ClassLoader A != Class A loaded by ClassLoader B 

How is it helpful?

It is helpful for defining different protection and access policies for different classloaders. Take an example of applet which is loaded using a different classloader, you would not want a third party application all access to your resources. So for security its important to maintain different namespaces.

like image 22
Narendra Pathai Avatar answered Sep 24 '22 19:09

Narendra Pathai