Skip to main content

Development

Java Class Loader

Introduction

The Java programs run on a Java virtual machine (JVM). This means that compiled programs are represented using a hardware- and operating system-independent binary format, typically (but not necessarily) stored in a file, known as the class file format.

In particular, a Java program isn’t a single executable file, but instead is composed of many individual class files, each of which corresponds to a single Java class.

Additionally, these class files are not loaded into memory all at once, but rather are loaded on demand, as needed by the program. The ClassLoader is the part of the JVM that loads classes into memory.

The Java ClassLoader is written in the Java language itself. This means that it’s easy to create your own ClassLoader without having to understand the finer details of the JVM.

ClassLoader

There are two kinds of class loaders: the bootstrap class loader supplied by the Java virtual machine, and user-defined class loaders. Every user-defined class loader is an instance of a subclass of the abstract class ClassLoader.

When the JVM is started, three class loaders are used:

  • Bootstrap class loader
  • Extensions class loader
  • System class loader

The bootstrap class loader loads the core Java librarieslocated in the <JAVA_HOME>/lib directory. This class loader, which is part of the core JVM, is written in native code.

The extensions class loader loads the code in the extensions directories (<JAVA_HOME>/lib/ext, or any other directory specified by the java.ext.dirs system property). It is implemented by the sun.misc.Launcher$ExtClassLoader class.

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

Applications implement subclasses of ClassLoader in order to extend the manner in which the Java virtual machine dynamically loads classes.

When user-defined class loader L loads class C, first, it will check whether the class has already been loaded. If C has not been loaded and L has a parent class loader, L will delegate duty to its parent class loader, L’s parents do the same. If C has not been loaded and L has not parent, L will delegate duty to bootstrap class loader. When L’s parents and bootstrap class loader load class C failed, an exception will be caught. Then L will load class C by itself.

Code:

Resources

ClassLoader http://docs.oracle.com/javase/7/docs/api/index.html

The class File Format http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html

Java Virtual Machine Specification – “Loading, Linking, and Initializing” http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-5.html

Tags

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Categories
Follow Us
TwitterLinkedinFacebookYoutubeInstagram