Java Classloader

The Java Class Loader, part of the Java Runtime Environment, dynamically loads Java classes into the Java Virtual Machine.[1] Usually classes are only loaded on demand. The virtual machine will only load the class files required for executing the program.[2] The Java run time system does not need to know about files and file systems as this is delegated to the class loader.

A software library is a collection of related object code. In the Java language, libraries are typically packaged in JAR files. Libraries can contain objects of different types. The most important type of object contained in a Jar file is a Java class. A class can be thought of as a named unit of code. The class loader is responsible for locating libraries, reading their contents, and loading the classes contained within the libraries. This loading is typically done "on demand", in that it does not occur until the class is called by the program. A class with a given name can only be loaded once by a given class loader.

Each Java class must be loaded by a class loader.[3][4] 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.

When the JVM is started, three class loaders are used:[5][6][2]

  1. Bootstrap class loader
  2. Extensions class loader
  3. System class loader

The bootstrap class loader loads the core Java libraries[fn 1] located in the <JAVA_HOME>/jre/lib (or <JAVA_HOME>/jmods> for Java 9 and above) directory. This class loader, which is part of the core JVM, is written in native code. The bootstrap class loader is not associated with any ClassLoader object.[2] For instance, StringBuilder.class.getClassLoader() returns null.[2]

The extensions class loader loads the code in the extensions directories (<JAVA_HOME>/jre/lib/ext,[5] or any other directory specified by the java.ext.dirs system property).

The system class loader loads code found on java.class.path, which maps to the CLASSPATH environment variable.

  1. ^ Mcmanis, Chuck (October 1, 1996). "The basics of Java class loaders". JavaWorld. Retrieved 2020-07-13.
  2. ^ a b c d Horstmann 2022, §10.1.1 The Class-Loading Process.
  3. ^ Horstmann 2022, §8.2.5 Writing Byte Codes to Memory.
  4. ^ Christudas, Binildas (January 26, 2005). "Internals of Java Class Loading". onjava.com. Archived from the original on 2018-05-10.
  5. ^ a b "Understanding Extension Class Loading". The Java Tutorials. docs.oracle.com. Retrieved 2020-07-13.
  6. ^ Sosnoski, Dennis (April 29, 2003). "Classes and class loading". IBM DeveloperWorks. Retrieved 2008-01-26.


Cite error: There are <ref group=fn> tags on this page, but the references will not show without a {{reflist|group=fn}} template (see the help page).


© MMXXIII Rich X Search. We shall prevail. All rights reserved. Rich X Search