Java Classloader
The Java Class Loader is a part of the Java Runtime Environment that 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]
- Bootstrap class loader
- Extensions class loader
- 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.
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. Apart from the Bootstrap class loader, every Java class loader has a parent class loader.[7] The parent class loader is 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 resource). This is an important feature for:
- implementing scripting languages, such as Jython
- using bean builders
- allowing user-defined extensibility
- allowing multiple namespaces to communicate. This is one of the foundations of CORBA / RMI protocols for example.
- to change the way the bytecode is loaded (for example, it is possible to use encrypted Java class bytecode[8]).
- to modify the loaded bytecode (for example, for load-time weaving of aspects when using aspect-oriented programming).
Class loaders in Jakarta EE
Jakarta EE (formerly Java EE and J2EE) application servers typically load classes from a deployed WAR or EAR archive by a tree of class loaders, isolating the application from other applications, but sharing classes between deployed modules. So-called "servlet containers" are typically implemented in terms of multiple class loaders.[4][9]
JAR hell
JAR hell is a term similar to DLL hell used to describe all the various ways in which the classloading process can end up not working.[10] 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.[11]
The OSGi 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, 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 — JSR 277 was initiated in 2005. The resolution — Java Platform Module System — intended to introduce a new distribution format, a modules versioning scheme, and a common modules repository (similar in purpose to Microsoft .NET's Global Assembly Cache). In December 2008, Sun announced that JSR 277 was put on hold.[12] The Java Module System was later rebooted as "project Jigsaw"[13] which was included in Java 9. 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.[14]
See also
Footnotes
References
- ↑ Mcmanis, Chuck (1996-10-01). "The basics of Java class loaders". JavaWorld. https://www.infoworld.com/article/2077260/learn-java-the-basics-of-java-class-loaders.html. Retrieved 2020-07-13.
- ↑ 2.0 2.1 2.2 2.3 Horstmann 2022, §10.1.1 The Class-Loading Process.
- ↑ Horstmann 2022, §8.2.5 Writing Byte Codes to Memory.
- ↑ 4.0 4.1 Christudas, Binildas (2005-01-26). "Internals of Java Class Loading". onjava.com. Archived from the original on 2018-05-10. https://web.archive.org/web/20180510223447/http://www.onjava.com/pub/a/onjava/2005/01/26/classloading.html.
- ↑ 5.0 5.1 "Understanding Extension Class Loading". docs.oracle.com. https://docs.oracle.com/javase/tutorial/ext/basics/load.html. Retrieved 2020-07-13.
- ↑ Sosnoski, Dennis (2003-04-29). "Classes and class loading". IBM DeveloperWorks. http://www.ibm.com/developerworks/java/library/j-dyn0429/. Retrieved 2008-01-26.
- ↑ Horstmann 2022, 10.1.2 The Class Loader Hierarchy.
- ↑ Roubtsov, Vladimir (2003-05-09). "Cracking Java byte-code encryption". JavaWorld. https://www.infoworld.com/article/2077342/cracking-java-byte-code-encryption.html. Retrieved 2020-07-13.
- ↑ deBoer, Tim; Karasiuk, Gary (2002-08-21). "J2EE Class Loading Demystified". IBM DeveloperWorks. https://www.ibm.com/developerworks/websphere/library/techarticles/0112_deboer/deboer.html. Retrieved 2008-01-26.
- ↑ "Depot - Apache Incubator". http://incubator.apache.org/depot/version/jar-hell.html.
- ↑ "Taxonomy of class loader problems with Jakarta Commons Logging". http://articles.qos.ch/classloader.html.
- ↑ Mark Reinhold (2010-09-20). "Project Jigsaw". Oracle Corporation. https://blogs.oracle.com/mr/entry/jigsaw.
- ↑ "Project Jigsaw". Oracle Corporation. http://openjdk.java.net/projects/jigsaw/. Retrieved 2015-11-29.
- ↑ Bartlett, Neil; Hackbarth, Kai (2016-09-22). "Java 9, OSGi and the Future of Modularity (Part 1)". InfoQ. https://www.infoq.com/articles/java9-osgi-future-modularity/#1.
External links
- Chuck McManis, "The basics of Java class loaders", 1996
- Brandon E. Taylor, "Java Class Loading: The Basics ", 2003
- Horstmann, Cay (April 15, 2022). Core Java. Oracle Press Java. ISBN 0-13-787107-4.
- Jeff Hanson, "Take Control of Class Loading in Java ", 2006-06-01
- Andreas Schaefer, "Inside Class Loaders", 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, "Real-World Use For Custom ClassLoaders", May 2005
- Christoph G. Jung, "Classloaders Revisited Hotdeploy", Java Specialist Newsletter, 2001-06-07
- Don Schwarz, "Managing Component Dependencies Using ClassLoaders", 2005-04-13
Original source: https://en.wikipedia.org/wiki/Java Classloader.
Read more |