Skip to main content

The Basics of Classloading in Java

The Java Classloader is a part of the Java Runtime Environment that dynamically loads Java classes into the Java Virtual Machine.

classes are loaded on demand in java run time environment. There is concept called delegation which is used in java class loading.

Compiled java classes (.class file)  are packaged in JAR files in the Java language. A class with a given name can only be loaded once by a given classloader.

Each Java class must be loaded by a class loader. Furthermore, Java programs may make use of external libraries (that is, libraries written and provided by someone other than the author of the program) or they may be composed, at least in part, of a number of libraries.

There are three class loaders which are responsible for Classloadng inside JVM -

1) Bootstrap class loader:


  • The bootstrap class loader loads the core Java libraries located in the /jre/lib directory. It loads the core classes which is available in rt.jar file also called runtime jar.
  • It is a part of the core JVM, is written in native code.


2) Extensions class loader:

  • The extensions class loader loads the code in the extensions directories /jre/lib/ext or any other directory specified by the java.ext.dirs system property.
  • It is implemented in java by sun.misc.Launcher$ExtClassLoader class.


3) System class loader:

  • The system class loader loads code found on java.class.path, which maps to the CLASSPATH environment variable.
  • This is implemented by the sun.misc.Launcher$AppClassLoader class.


Classloading happens based on delegation concept. To understand refer the upper diagram. Application class loader delegates and ask extension class loader to load class and extension class loader delegates the same to bootstrap class loader.

If bootstrap class loader doesn't find the class and fails to load then the latter class loader tries to loads the required class.

Lets see the below code to understand how the class loading happens -

package com.refermynotes;

import java.net.URLClassLoader;

public class DelegationExample {
public static void main(String[] args) {
URLClassLoader classLoader =  (URLClassLoader) ClassLoader.getSystemClassLoader();
do{
System.out.println(classLoader);
}while((classLoader = (URLClassLoader)classLoader.getParent()) != null);
System.out.println("Bootstrap classloader");
}
}

Output :
sun.misc.Launcher$AppClassLoader@73d16e93
sun.misc.Launcher$ExtClassLoader@15db9742
Bootstrap classloader


Comments

Popular posts from this blog

StackOverFlowError and OutOfMemoryError in java

There are two area inside java virtual machine's memory the heap and the  stack . The  stack  memory is used to store local variables and function call while heap memory is used to store objects in  Java The most common cause of StackOverFlowError is too deep or infinite recursion or many local objects / variables creation inside function call in  Java.  According to the java source documentation,  Java throws  java.lang.StackOverflowError   when a stack overflow occurs because an application recurses too deeply. JVM has a given memory allocation for each stack of each thread, and if an attempt to call a method happens to fill this memory, JVM throws an error. Just like it would do if we try to write at index N of an array of length N.  The point to be noted here is that - These are errors not an exceptions. No memory corruption happens due to the error. Stack can not write into the heap space. A StackOverflowError i...

Job Sequencing with Deadlines

Given a set of n jobs Each job i has an integer deadlines di>=0 and a profit pi>0 All jobs requires only one unit time to complete Only one machine is available for processing jobs For job i the profit pi is earned if the job is completed by its deadline.