Java Classloader
   HOME

TheInfoList



OR:

The Java Class Loader is a part of the
Java Runtime Environment Java is a set of computer software and specifications developed by James Gosling at Sun Microsystems, which was later acquired by the Oracle Corporation, that provides a system for developing application software and deploying it in a cros ...
that dynamically loads
Java class A Java class file is a file (with the filename extension) containing Java bytecode that can be executed on the Java Virtual Machine (JVM). A Java class file is usually produced by a Java compiler from Java programming language source files ( fi ...
es into the
Java Virtual Machine A Java virtual machine (JVM) is a virtual machine that enables a computer to run Java programs as well as programs written in other languages that are also compiled to Java bytecode. The JVM is detailed by a specification that formally describes ...
. Usually classes are only loaded
on demand On-demand or on demand may refer to: Manufacturing * Build-on-demand * Just-in-time manufacturing, a methodology for production * Print on demand, printing technology and business process in which new copies of a document are not printed until an ...
. 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 In computer science, a library is a collection of non-volatile resources used by computer programs, often for software development. These may include configuration data, documentation, help data, message templates, pre-written code and subr ...
is a collection of related
object code In computing, object code or object module is the product of a compiler. In a general sense object code is a sequence of statements or instructions in a computer language, usually a machine code language (i.e., binary) or an intermediate langua ...
. In the
Java language Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. It is a general-purpose programming language intended to let programmers ''write once, run any ...
, 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 Java class file is a file (with the filename extension) containing Java bytecode that can be executed on the Java Virtual Machine (JVM). A Java class file is usually produced by a Java compiler from Java programming language source files ( fi ...
. 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. Furthermore,
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
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: # Bootstrap class loader # Extensions class loader # System class loader The bootstrap class loader loads the core Java librariesThese libraries are stored in Jar files called ''rt.jar'', ''core.jar'', ''server.jar'', etc. located in the /jre/lib (or /jmods> for Java 9 and above) 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 (/jre/lib/ext, 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 An environment variable is a dynamic-named value that can affect the way running processes will behave on a computer. They are part of the environment in which a process runs. For example, a running process can query the value of the TEMP env ...
.


User-defined class loaders

The Java class loader is written in Java. It is therefore possible to create your own class loader without understanding the finer details of the Java Virtual Machine. Every Java class loader has a parent class loader, defined when a new class loader is instantiated or set to the virtual machine's system default class loader. This makes it possible (for example): * to load or unload classes at runtime (for example to load libraries dynamically at runtime, even from an
HTTP The Hypertext Transfer Protocol (HTTP) is an application layer protocol in the Internet protocol suite model for distributed, collaborative, hypermedia information systems. HTTP is the foundation of data communication for the World Wide Web, ...
resource). This is an important feature for: ** implementing scripting languages, such as
Jython Jython is an implementation of the Python programming language designed to run on the Java platform. The implementation was formerly known as JPython until 1999. Overview Jython programs can import and use any Java class. Except for some standa ...
** using
bean A bean is the seed of several plants in the family Fabaceae, which are used as vegetables for human or animal food. They can be cooked in many different ways, including boiling, frying, and baking, and are used in many traditional dishes th ...
builders ** allowing user-defined extensibility ** allowing multiple
namespace In computing, a namespace is a set of signs (''names'') that are used to identify and refer to objects of various kinds. A namespace ensures that all of a given set of objects have unique names so that they can be easily identified. Namespaces ...
s to communicate. This is one of the foundations of
CORBA The Common Object Request Broker Architecture (CORBA) is a standard defined by the Object Management Group (OMG) designed to facilitate the communication of systems that are deployed on diverse platforms. CORBA enables collaboration between sys ...
/ RMI protocols for example. * to change the way the
bytecode Bytecode (also called portable code or p-code) is a form of instruction set designed for efficient execution by a software interpreter. Unlike human-readable source code, bytecodes are compact numeric codes, constants, and references (norma ...
is loaded (for example, it is possible to use
encrypted In cryptography, encryption is the process of encoding information. This process converts the original representation of the information, known as plaintext, into an alternative form known as ciphertext. Ideally, only authorized parties can decip ...
Java class bytecode). * to modify the loaded bytecode (for example, for load-time
weaving Weaving is a method of textile production in which two distinct sets of yarns or threads are interlaced at right angles to form a fabric or cloth. Other methods are knitting, crocheting, felting, and braiding or plaiting. The longitudinal th ...
of aspects when using
aspect-oriented programming In computing, aspect-oriented programming (AOP) is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. It does so by adding behavior to existing code (an advice) ''without'' modifying t ...
).


Class Loaders in Jakarta EE

Jakarta EE Jakarta EE, formerly Java Platform, Enterprise Edition (Java EE) and Java 2 Platform, Enterprise Edition (J2EE), is a set of specifications, extending Java SE with specifications for enterprise features such as distributed computing and web serv ...
(formerly Java EE and J2EE) application servers typically load classes from a deployed
WAR War is an intense armed conflict between states, governments, societies, or paramilitary groups such as mercenaries, insurgents, and militias. It is generally characterized by extreme violence, destruction, and mortality, using regular o ...
or
EAR An ear is the organ that enables hearing and, in mammals, body balance using the vestibular system. In mammals, the ear is usually described as having three parts—the outer ear, the middle ear and the inner ear. The outer ear consists of ...
archive by a tree of class loaders, isolating the application from other applications, but sharing classes between deployed modules. So-called "
servlet container A web container (also known as a servlet container; and compare "webcontainer" ) is the component of a web server that interacts with Jakarta Servlets. A web container is responsible for managing the lifecycle of servlets, mapping a URL to a pa ...
s" are typically implemented in terms of multiple class loaders.


JAR hell

JAR hell is a term similar to
DLL hell In computing, DLL Hell is a term for the complications that arise when one works with dynamic-link libraries (DLLs) used with Microsoft Windows operating systems, particularly legacy 16-bit editions, which all run in a single memory space. DLL Hel ...
used to describe all the various ways in which the classloading process can end up not working. Three ways JAR hell can occur are: * Accidental presence of two different versions of a library installed on a system. This will not be considered an error by the system. Rather, the system will load classes from one or the other library. Adding the new library to the list of available libraries instead of replacing it may result in the application still behaving as though the old library is in use, which it may well be. * Multiple libraries or applications require different versions of library foo. If versions of library foo use the same class names, there is no way to load the versions of library foo with the same class loader. * The most complex JAR hell problems arise in circumstances that take advantage of the full complexity of the classloading system. A Java program is not required to use only a single "flat" class loader, but instead may be composed of several (potentially very many) nested, cooperating class loaders. Classes loaded by different class loaders may interact in complex ways not fully comprehended by a developer, leading to errors or bugs that are difficult to analyze, explain, and resolve. The
OSGi OSGi is an open specification and open source project under the Eclipse Foundation. It is a continuation of the work done by the OSGi Alliance (formerly known as the Open Services Gateway initiative), which was an open standards organization for ...
Alliance specified (starting as JSR 8 in 1998) a modularity framework that aims to solve JAR hell for current and future VMs in ME, SE, and EE that is widely adopted. Using metadata in the JAR
manifest Manifest may refer to: Computing * Manifest file, a metadata file that enumerates files in a program or package * Manifest (CLI), a metadata text file for CLI assemblies Events * Manifest (convention), a defunct anime festival in Melbourne, Aus ...
, JAR files (called bundles) are wired on a per-package basis. Bundles can export packages, import packages and keep packages private, providing the basic constructs of modularity and versioned dependency management. To remedy the JAR hell problems, a
Java Community Process The Java Community Process (JCP), established in 1998, is a formalized mechanism that allows interested parties to develop standard technical specifications for Java technology. Anyone can become a JCP Member by filling a form available at thJCP w ...
 — JSR 277 was initiated in 2005. The resolution —
Java Platform Module System The Java Platform Module System specifies a distribution format for collections of Java code and associated resources. It also specifies a repository for storing these collections, or ''modules'', and identifies how they can be discovered, loaded a ...
 — intended to introduce a new distribution format, a modules versioning scheme, and a common modules repository (similar in purpose to
Microsoft .NET The Microsoft .NET strategy is a marketing plan that Microsoft followed in the early 2000s. Steve Ballmer described it as the company's "most ambitious undertaking since Internet Strategy Day in 1995". In support of this strategy, between 2000 and ...
's
Global Assembly Cache The Global Assembly Cache (GAC) is a machine-wide CLI assembly cache for the Common Language Infrastructure (CLI) in Microsoft's .NET Framework. The approach of having a specially controlled central repository addresses the flaws in the shared lib ...
). In December 2008, Sun announced that JSR 277 was put on hold. The Java Module System was later rebooted as "project Jigsaw" which was included in
Java 9 The Java language has undergone several changes since JDK 1.0 as well as numerous additions of classes and packages to the standard library. Since J2SE 1.4, the evolution of the Java language has been governed by the Java Community P ...
. Released in 2017, it includes support for modular software, termed "Java Platform Module System", which is controlled on source level with module-info.java files. It follows a different philosophy from the OSGi architecture that aims at providing modularity for the Java Runtime Environment in a backwards-compatible way that uses the default mechanism of loading classes that the JRE provides. However, since it does not offer the ability for controlled co-existence of libraries with different versions, it is not suited for tackling the JAR Hell problem.


See also

*
Loader (computing) In computer systems a loader is the part of an operating system that is responsible for loading programs and libraries. It is one of the essential stages in the process of starting a program, as it places programs into memory and prepares them ...
*
Dynamic loading Dynamic loading is a mechanism by which a computer program can, at run time, load a library (or other binary) into memory, retrieve the addresses of functions and variables contained in the library, execute those functions or access those varia ...
*
DLL hell In computing, DLL Hell is a term for the complications that arise when one works with dynamic-link libraries (DLLs) used with Microsoft Windows operating systems, particularly legacy 16-bit editions, which all run in a single memory space. DLL Hel ...
*
OSGi OSGi is an open specification and open source project under the Eclipse Foundation. It is a continuation of the work done by the OSGi Alliance (formerly known as the Open Services Gateway initiative), which was an open standards organization for ...
*
Classpath (Java) Classpath is a parameter in the Java Virtual Machine or the Java compiler that specifies the location of user-defined classes and packages. The parameter may be set either on the command-line, or through an environment variable. Overview and ...
*
Java Platform Module System The Java Platform Module System specifies a distribution format for collections of Java code and associated resources. It also specifies a repository for storing these collections, or ''modules'', and identifies how they can be discovered, loaded a ...


Footnotes


References


External links

* Chuck McManis,
The basics of Java class loaders
, 1996 * Brandon E. Taylor,
Java Class Loading: The Basics
, 2003 * Jeff Hanson,
Take Control of Class Loading in Java
, 2006-06-01 * Andreas Schaefer,

, 2003-11-12 * Sheng Liang and Gilad Bracha,
Dynamic class loading in the Java virtual machine
, In Proceedings of the 13th ACM Conference on Object-Oriented Programming, Systems, Languages, and Applications (OOPSLA'98), ACM SIGPLAN Notices, vol. 33, no. 10, ACM Press, 1998, pp. 36–44 {{doi, 10.1145/286936.286945 * Jeremy Whitlock,

, May 2005 * Dr. Christoph G. Jung,
Classloaders Revisited Hotdeploy
, ''Java Specialist Newsletter'', 2001-06-07 * Don Schwarz,

, 2005-04-13 Java (programming language)