Java Classloader
From Wikipedia, the free encyclopedia
The Java Classloader is a part of the Java Runtime Environment that dynamically loads Java classes into the Java Virtual Machine. Usually classes are only loaded on demand. The Java run time system does not need to know about files and file systems because of class loaders. Delegation is an important concept to understand when learning about class loaders.
A software library is a collection of more or less related object code. In the Java language, libraries are typically packaged in Jar files. Libraries can contain various different sorts of objects, 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 actually used by the program. A class with a given name can only be loaded once by a given classloader.
The classloading process is fairly complicated, and is the subject of much confusion in software deployment and development. Java programs may make use of external or third party libraries (that is, libraries written and provided by someone other than the author of the program) or may itself be composed, at least in part, by a number of libraries.
Each Java class must be loaded by a class loader. 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 libraries, i.e. core.jar, server.jar etc. 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 loading code found on java.class.path, which maps to the system CLASSPATH variable. This is implemented by the sun.misc.Launcher$AppClassLoader class.
[edit] JAR hell
Jar hell is a general term used to describe all the various ways in which the classloading process can end up not working.
- One case is when a developer or deployer of a Java application has accidentally made two different versions of a library available to the system. This is not considered an error by the system. Rather, the system will load classes from one or the other library. So, a developer who thinks he has replaced a library with a new version, but who has instead simply added the new library to the list of available libraries, may be surprised to see the application still behaving as though the old library is in use, which of course, it may well be.
- Another version of the problem arises when two libraries (or a library and the application) require different versions of the same third library. Without taking extraordinary measures (which may or may not even be available, depending on the circumstances) there is no way to load both versions of the third library.
- 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" classloader, but instead may be composed of several (an indefinite number, in fact) nested, cooperating classloaders. This is not as uncommon as one might think: so-called "servlet containers" are typically implemented in terms of multiple classloaders. The interactions between classloaders is too complex to be fully described here. It is sufficient to say that classes loaded by different classloaders interact in ways which may not be fully comprehended by the developer, leading to inexplicable errors or bugs.
[edit] See also
- Loader (computing)
- DLL hell
- Apache Maven, automated software build tool with dependency management
[edit] External links
- Article "The basics of Java class loaders" by Chuck Mcmanis
- Article "Java Class Loading: The Basics" by Brandon E. Taylor
- Article "Take Control of Class Loading in Java" by Jeff Hanson
- Article "Java programming dynamics, Part 1: Classes and class loading" by Dennis Sosnoski
- Article "J2EE Class Loading Demystified" by Tim deBoer and Gary Karasiuk
- Article "Internals of Java Class Loading" by Binildas Christudas
- Article "Inside Class Loaders" by Andreas Schaefer
- Article "Dynamic class loading in the Java virtual machine" by Sheng Liang and Gilad Bracha
- API description
- Understanding Extension Class Loading from Sun's Java Tutorial
- Dependency manager - now at Apache incubator
- Dynamic loading of classes directly from JAR files