Java Classloader

Java Classloader

The Java Classloader 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 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 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 actually used by the program. A class with a given name can only be loaded once by a given classloader.

Contents

Class loading process

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

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

The bootstrap class loader loads the core Java libraries[5] (<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. This is implemented by the sun.misc.Launcher$AppClassLoader class.

User-defined class loaders

By default, all user classes are loaded by the default system class loader, but it is possible to replace it by a user-defined ClassLoader, and even to chain class loaders as desired.[clarification needed]

This makes it possible (for example):

  • to load or unload classes at runtime (for example to load libraries dynamically at runtime, even from a HTTP resource). This is an important feature for:
    • implementing scripting languages,
    • 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[6]).
  • to modify the loaded bytecode (for example, for load-time weaving of aspects when using aspect-oriented programming).

Class Loaders in JEE

Java Platform, Enterprise Edition (JEE) application servers typically load classes from a deployed WAR or EAR archive by a tree of classloaders, isolating the application from other applications, but sharing classes between deployed modules. So-called "servlet containers" are typically implemented in terms of multiple classloaders.[2][7]

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.[8] Three ways JAR hell can occur are:

  • The first 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 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 see the application still behaving as though the old library is in use, which 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. If both versions of the third library use the same class names, there is no way to load both versions of the third library with the same classloader.
  • 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 (potentially very many) nested, cooperating classloaders. Classes loaded by different classloaders may interact in complex ways not fully comprehended by a developer, leading to inexplicable errors or bugs[citation needed].

The OSGi Alliance specified (starting as JSR 8 in 1998) a modularity framework that solved 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 Module System — intended to introduce a new distribution format, 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.[9]

See also

References

  1. ^ Mcmanis, Chuck (1996-10-01). "The basics of Java class loaders". JavaWorld. http://www.javaworld.com/javaworld/jw-10-1996/jw-10-indepth.html. Retrieved 2008-01-26. 
  2. ^ a b Christudas, Binildas (2005-01-26). "Internals of Java Class Loading". onjava.com. http://www.onjava.com/pub/a/onjava/2005/01/26/classloading.html. Retrieved 2009-10-02. 
  3. ^ "Understanding Extension Class Loading". java.sun.com. 2008-02-14. http://java.sun.com/docs/books/tutorial/ext/basics/load.html. Retrieved 2009-12-08. 
  4. ^ Sosnoski, Dennis (2003-04-29). "Classes and class loading". ibm.com. http://www.ibm.com/developerworks/java/library/j-dyn0429/. Retrieved 2008-01-26. 
  5. ^ These libraries are stored in Jar files called rt.jar, core.jar, server.jar, etc.
  6. ^ Roubtsov, Vladimir (2003-05-09). "Cracking Java byte-code encryption". javaworld.com. http://www.javaworld.com/javaworld/javaqa/2003-05/01-qa-0509-jcrypt.html?page=1. Retrieved 2008-01-26. 
  7. ^ deBoer, Tim; Karasiuk, Gary (2002-08-21). "J2EE Class Loading Demystified". ibm.com. http://www-128.ibm.com/developerworks/websphere/library/techarticles/0112_deboer/deboer.html. Retrieved 2008-01-26. 
  8. ^ http://incubator.apache.org/depot/version/jar-hell.html
  9. ^ http://www.osgi.org/News/20081217

External links


Wikimedia Foundation. 2010.

Игры ⚽ Нужно сделать НИР?

Look at other dictionaries:

  • Java classloader — El Java Classloader( en español, cargador de clases Java) es una parte del Java Runtime Environment que carga dinámicamente clases Java en la Java Virtual Machine. Normalmente las clases solo son cargadas bajo demanda. Una biblioteca de software… …   Wikipedia Español

  • Classloader — Chargeur de classe Java Le chargeur de classe java (Java Classloader) est une partie du JRE (Java Runtime Environment) qui charge dynamiquement les classes java dans la machine virtuelle java. En général, les classes sont chargées seulement à la… …   Wikipédia en Français

  • Java Platform, Standard Edition — Java Card Micro Edition (ME) Standard Edition (SE) …   Википедия

  • Java VM — Java Logo Die Java Virtual Machine (abgekürzt Java VM oder JVM) ist der Teil der Java Laufzeitumgebung (JRE) für Java Programme, der für die Ausführung des Java Bytecodes verantwortlich ist. Hierbei wird im Normalfall jedes gestartete Java… …   Deutsch Wikipedia

  • Java Virtual Machine — Java Logo Die Java Virtual Machine (abgekürzt Java VM oder JVM) ist der Teil der Java Laufzeitumgebung (JRE) für Java Programme, der für die Ausführung des Java Bytecodes verantwortlich ist. Hierbei wird im Normalfall jedes gestartete Java… …   Deutsch Wikipedia

  • Chargeur de classe Java — Le chargeur de classe java (Java Classloader) est une partie du JRE (Java Runtime Environment) qui charge dynamiquement les classes java dans la machine virtuelle java. En général, les classes sont chargées seulement à la demande. Le JRE n a pas… …   Wikipédia en Français

  • Classpath (Java) — This article is about the argument on the command line of Java programs. For the Free Software Foundation s implementation of the Java standard library, see GNU Classpath. Classpath is a parameter set either on the command line, or through an… …   Wikipedia

  • EAR (file format) — An Enterprise ARchive, or EAR, is a file format used by Java EE for packaging one or more modules into a single archive so that the deployment of the various modules onto an application server happens simultaneously and coherently. It also… …   Wikipedia

  • 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 variables, and unload… …   Wikipedia

  • Object-capability model — The object capability model is a computer security model based on the Actor model of computation. The name object capability model is due to the idea that the capability to perform an operation can be obtained by the following combination: an… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”