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 classloaders. Delegation is an important concept to understand when learning about classloaders.

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 classloader.

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] located in the <JAVA_HOME>/jre/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>/jre/lib/ext,[6] 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 CLASSPATH environment variable. This is implemented by the sun.misc.Launcher$AppClassLoader class.

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):

Class Loaders in Java EE

Java Platform, Enterprise Edition (Java EE) 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][8]

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

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 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.[11] The Java Module System was later rebooted as "project Jigsaw"[12] which should be included in Java 9.

See also

References

  1. Mcmanis, Chuck (1996-10-01). "The basics of Java class loaders". JavaWorld. Retrieved 2008-01-26.
  2. 1 2 Christudas, Binildas (2005-01-26). "Internals of Java Class Loading". onjava.com. Retrieved 2009-10-02.
  3. "Understanding Extension Class Loading". java.sun.com. 2008-02-14. Retrieved 2009-12-08.
  4. Sosnoski, Dennis (2003-04-29). "Classes and class loading". ibm.com. Retrieved 2008-01-26.
  5. These libraries are stored in Jar files called rt.jar, core.jar, server.jar, etc.
  6. http://docs.oracle.com/javase/tutorial/ext/basics/load.html
  7. Roubtsov, Vladimir (2003-05-09). "Cracking Java byte-code encryption". javaworld.com. Retrieved 2008-01-26.
  8. deBoer, Tim; Karasiuk, Gary (2002-08-21). "J2EE Class Loading Demystified". ibm.com. Retrieved 2008-01-26.
  9. http://incubator.apache.org/depot/version/jar-hell.html
  10. http://articles.qos.ch/classloader.html
  11. Mark Reinhold (2010-09-20). "Project Jigsaw". Oracle Corporation. Retrieved 2015-11-29.
  12. "Project Jigsaw". Oracle Corporation. Retrieved 2015-11-29.

External links

This article is issued from Wikipedia - version of the Wednesday, March 23, 2016. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.