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

Optimal Binary Search using Dynamic Programming

An optimal binary search tree is a binary search tree for which the nodes are arranged on levels such that the tree cost is minimum. If the probabilities of searching for elements of a set are known from accumulated data from past searches, then Binary Search Tree (BST) should be such that the average number of comparisons in a search should be minimum. eg. Lets the elements to be searched are A, B, C, D and probabilities of searching these items are 0.1, 0.2, 0.4 and 0.3 respectively. Lets consider 2 out of 14 possible BST containing these keys. Figure 1 Figure 2 Average number of comparison is calculated as sum of level*probability(key element) for each element of the tree. Lets the level of tree start from 1. Therefore, for figure 1 -     Average number of comparison = 1*0.1 +2*0.2 +3*0.4 +4*0.3  = 2.9                                  ...

Important points on Classes and Methods in Java as per Java Language Specification

Class Declarations: A class declaration specifies a new named reference type. There are two kinds of class declarations: normal class declarations and enum declarations. It is a compile-time error if a class has the same simple name as any of its enclosing classes or interfaces. Class Modifiers: A class declaration may include class modifiers. The access modifier public pertains only to top level classes and member classes, not to local classes or anonymous classes. The access modifiers protected and private pertain only to member classes within a directly enclosing class declaration. The modifier static pertains only to member classes, not to top level or local or anonymous classes. It is a compile-time error if the same keyword appears more than once as a modifier for a class declaration. abstract Classes: An abstract class is a class that is incomplete, or to be considered incomplete. It is a compile-time error if an attempt is made to create an instance o...

Travel Sales Man Problem (TSP) and Solution using Dynamic Programming

Given a graph of n vertices, determine the minimum cost path to start at a given vertex and travel to each other vertex exactly once, returning to the starting vertex. In some versions, the starting and ending points are different and fixed, and all other points have to be visited exactly once from start to end. A standard way to solve these problems is to try all possible orders of visiting the n points, which results in a run-time of O(n!). To calculate cost using Dynamic Programming, we need to establish recursive relation in terms of sub-problems. Let us define a term C(S, i) be the cost of the minimum cost path visiting each vertex in set S exactly once, starting at 1 and ending at i. We start with all subsets of size 2 and calculate C(S, i) for all subsets where S is the subset, then we calculate C(S, i) for all subsets S of size 3 and so on. Note that 1 must be present in every subset. So the algorithm is like below - If size of S is 2, then S must be {1, i},  C(S, ...