Java class loader: Difference between revisions

Content deleted Content added
m JAR hell: punctuation
 
(21 intermediate revisions by 15 users not shown)
Line 1:
The '''Java Classclass Loaderloader''' is a, part of the [[Java Runtime Environment]] that, [[Dynamic loading|dynamically loads]] [[Java class]]es into the [[Java virtual machine|Java Virtual Machine]].<ref>{{cite web
|last1=Mcmanis |first1=Chuck
|date=1996-10-01 |df=mdy
Line 6:
|work=[[JavaWorld]]
|accessdate=2020-07-13
}}</ref> Usually classes are only loaded [[lazy initialization|on demand]]. The virtual machine will only load the class files required for executing the program.{{sfn | Horstmann | 2022 | loc=§10.1.1 The Class-Loading Process}} The Java run time system does not need to know about files and file systems as this is [[Delegation _(object-oriented_programming)|delegated]] to the class loader.
 
A [[Library (computing)|software library]] is a collection of related [[object code]].
In the [[Java (programming language)|Java language]], libraries are typically packaged in [[JAR (file format)|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.{{sfn | Horstmann | 2022 | loc=§8.2.5 Writing Byte Codes to Memory}}<ref name="Christudas">{{cite web
|last1=Christudas |first1=Binildas
|date=2005-01-26 |df=mdy
Line 32:
|url=http://www.ibm.com/developerworks/java/library/j-dyn0429/
|title=Classes and class loading
|work=[[IBM DeveloperWorks]]
|accessdate=2008-01-26
}}</ref>{{sfn | Horstmann | 2022 | loc=§10.1.1 The Class-Loading Process}}
}}</ref>
# Bootstrap class loader
# Extensions class loader
# System class loader
 
The bootstrap class loader loads the core Java libraries<ref group=fn>These libraries are stored in [[JAR (file format)|Jar files]] called ''rt.jar'', ''core.jar'', ''server.jar'', etc.</ref> located in the <code><JAVA_HOME>/jre/lib</code> (or <code><JAVA_HOME>/jmods></code> 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 {{java|ClassLoader}} object.{{sfn | Horstmann | 2022 | loc=§10.1.1 The Class-Loading Process}} For instance, {{java|StringBuilder.class.getClassLoader()}} returns {{java|null}}.{{sfn | Horstmann | 2022 | loc=§10.1.1 The Class-Loading Process}}
 
The extensions class loader loads the code in the extensions directories (<code><JAVA_HOME>/jre/lib/ext</code>,<ref name="TJT:UECL"/> or any other directory specified
Line 47:
 
==User-defined class loaders==
The Java class loader is written in Java. It is therefore possible to create youra owncustom class loader without understanding the finer details of the Java Virtual Machine. EveryApart from the Bootstrap class loader, every Java class loader has a parent class loader,.{{sfn | Horstmann | 2022 | loc=10.1.2 The Class Loader Hierarchy}} 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):
Line 65:
* to modify the loaded bytecode (for example, for load-time [[aspect weaver|weaving]] of aspects when using [[aspect-oriented programming]]).
 
== Class Loadersloaders in Jakarta EE ==
[[Jakarta EE]] (formerly Java EE and J2EE) application servers typically load classes from a deployed [[WAR (file format)|WAR]] or [[EAR (file format)|EAR]] archive by a tree of class loaders, isolating the application from other applications, but sharing classes between deployed modules. So-called "[[servlet container]]s" are typically implemented in terms of multiple class loaders.<ref name="Christudas"/><ref>{{cite web
|last1=deBoer |first1=Tim
Line 72:
|url=https://www.ibm.com/developerworks/websphere/library/techarticles/0112_deboer/deboer.html
|title=J2EE Class Loading Demystified
|work=[[IBM DeveloperWorks]]
|accessdate=2008-01-26
}}</ref>
Line 84:
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 file|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.
 
}}</ref> which was included in [[Java version history#Java SE 9|Java 9]]. Releasedintroduced the [[Java Platform Module System]] in 2017,. itThis includesspecifies supporta distribution format for modularcollections softwareof Java code and associated resources. It also specifies a repository for storing these collections, termedor "Java''[[Modular Platformprogramming|modules]]'', Moduleand System"identifies how they can be discovered, whichloaded isand controlledchecked onfor sourceintegrity. levelIt includes features such as namespaces with module-info.javathe aim of fixing some of the shortcomings in the existing [[JAR (file format)|JAR]] filesformat. ItThe Java Platform Module System 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 itthe Java Platform Module System does not offer the ability for controlled co-existence of libraries with different versions, it isdoes not suited forfully tacklingaddress the JAR Hellhell problem.<ref>{{cite web|first1=Neil|last1=Bartlett|first2=Kai|last2=Hackbarth|title=Java 9, OSGi and the Future of Modularity (Part 1)
To remedy the JAR hell problems, a [[Java Community Process]]&nbsp;— JSR 277 was initiated in 2005. The resolution&nbsp;— [[Java Platform Module System]]&nbsp;— 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.<ref>{{cite web
|url=https://blogs.oracle.com/mr/entry/jigsaw
|title=Project Jigsaw
|publisher=[[Oracle Corporation]]
|author=Mark Reinhold
|date=2010-09-20 |df=mdy
|archive-url=https://web.archive.org/web/20151208171951/https://blogs.oracle.com/mr/entry/jigsaw
|archive-date=2015-12-08
|url-status=dead
}}</ref> The Java Module System was later rebooted as "project Jigsaw"<ref>{{cite web
|url=http://openjdk.java.net/projects/jigsaw/
|title=Project Jigsaw
|publisher=[[Oracle Corporation]]
|accessdate=2015-11-29
}}</ref> which was included in [[Java version history#Java SE 9|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 co-existence of libraries with different versions, it is not suited for tackling the JAR Hell problem.<ref>{{cite web|first1=Neil|last1=Bartlett|first2=Kai|last2=Hackbarth|title=Java 9, OSGi and the Future of Modularity (Part 1)
|url=https://www.infoq.com/articles/java9-osgi-future-modularity/#1|publisher=InfoQ|date=2016-09-22}}</ref>
 
== See also ==
* [[Loader (computing)]]
* [[Dynamic loading]]
Line 115 ⟶ 101:
{{reflist}}
 
== External links ==
* Chuck McManis, "[https://www.infoworld.com/article/2077260/learn-java-the-basics-of-java-class-loaders.html The basics of Java class loaders]", 1996
* Brandon E. Taylor, "[http://www.developer.com/java/other/article.php/2248831 Java Class Loading: The Basics] {{Webarchive|url=https://web.archive.org/web/20201109091535/http://www.developer.com/java/other/article.php/2248831 |date=2020-11-09 }}", 2003
* {{cite book | last=Horstmann | first=Cay | title=Core Java | publisher=Oracle Press Java | date=April 15, 2022 | isbn=0-13-787107-4}}
* Jeff Hanson, "[http://www.devx.com/Java/Article/31614 Take Control of Class Loading in Java] {{Webarchive|url=https://web.archive.org/web/20201204030013/http://www.devx.com/Java/Article/31614 |date=2020-12-04 }}", 2006-06-01
* Andreas Schaefer, "[https://web.archive.org/web/20180506212821/http://www.onjava.com/pub/a/onjava/2003/11/12/classloader.html Inside Class Loaders]", 2003-11-12
* Sheng Liang and Gilad Bracha, "[https://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.18.762 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.&nbsp;36–44 {{doi|10.1145/286936.286945}}
* Jeremy Whitlock, "[https://web.archive.org/web/20080628071703/http://dev2dev.bea.com/blog/jcscoobyrs/archive/2005/05/realworld_use_f.html Real-World Use For Custom ClassLoaders]", May 2005
* Dr. Christoph G. Jung, "[https://web.archive.org/web/20130201093532/http://www.roseindia.net/javatutorials/hotdeploy.shtml Classloaders Revisited Hotdeploy]", ''Java Specialist Newsletter'', 2001-06-07
* Don Schwarz, "[https://web.archive.org/web/20180506083919/http://www.onjava.com/pub/a/onjava/2005/04/13/dependencies.html Managing Component Dependencies Using ClassLoaders]", 2005-04-13