Content deleted Content added
Rescuing 1 sources and tagging 0 as dead.) #IABot (v2.0 |
m →JAR hell: punctuation |
||
(32 intermediate revisions by 22 users not shown) | |||
Line 1:
The '''Java
|last1=Mcmanis |first1=Chuck
| title=The basics of Java class loaders▼
|url=https://www.infoworld.com/article/2077260/learn-java-the-basics-of-java-class-loaders.html
| publisher=[[JavaWorld]]▼
▲ | date=1996-10-01
A [[Library (computing)|software library]] is a collection of related [[object code]].
In the [[Java (programming language)|Java language]], libraries
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
| url=http://www.onjava.com/pub/a/onjava/2005/01/26/classloading.html▼
| title=Internals of Java Class Loading▼
| publisher=onjava.com▼
|archiveurl=https://web.archive.org/web/20180510223447/http://www.onjava.com/pub/a/onjava/2005/01/26/classloading.html
▲ | date=2005-01-26
|archivedate=2018-05-10
| accessdate=2009-10-02 }}</ref> Furthermore, [[Java (software platform)|Java]] programs may make use of [[Library (computing)|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:<ref name="TJT:UECL">{{cite web
|
|
|department=The Java Tutorials
|work=docs.oracle.com
|
}}</ref><ref>{{cite web |last1=Sosnoski |first1=Dennis
| url=http://www.ibm.com/developerworks/java/library/j-dyn0429/▼
| title=Classes and class loading▼
|work=IBM DeveloperWorks
▲ | date=2003-04-29
|
}}</ref>{{sfn | Horstmann | 2022 | loc=§10.1.1 The Class-Loading Process}}
# 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
by the <code>java.ext.dirs</code> system property)
The system class loader loads code found on <code>java.class.path</code>, which maps to the <code>[[Classpath (Java)|CLASSPATH]]</code> [[environment variable]]
==User-defined class loaders==
The Java class loader is written in Java. It is therefore possible to create
This makes it possible (for example):
Line 51 ⟶ 56:
** allowing multiple [[namespace]]s to communicate. This is one of the foundations of [[Common Object Request Broker Architecture|CORBA]] / [[Java remote method invocation|RMI]] protocols for example.
* to change the way the [[Java bytecode|bytecode]] is loaded (for example, it is possible to use [[Encryption|encrypted]] Java class bytecode<ref>{{cite web
|last1=Roubtsov |first1=Vladimir
| title=Cracking Java byte-code encryption▼
|url=https://www.infoworld.com/article/2077342/cracking-java-byte-code-encryption.html
|work=[[JavaWorld]]
▲ | date=2003-05-09
|
}}</ref>). * to modify the loaded bytecode (for example, for load-time [[
== Class
[[
|last1=deBoer |first1=Tim
▲[[Java Platform, Enterprise Edition]] (Java EE) application servers typically load classes from a deployed [[WAR (Sun file format)|WAR]] or [[EAR (file format)|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.<ref name="Christudas"/><ref>{{cite web
|last2=Karasiuk |first2=Gary
| url=https://www.ibm.com/developerworks/websphere/library/techarticles/0112_deboer/deboer.html▼
|date=2002-08-21 |df=mdy
| title=J2EE Class Loading Demystified▼
|work=IBM DeveloperWorks
|
== {{anchor|jarhell}} 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.<ref>{{Cite web|url=http://incubator.apache.org/depot/version/jar-hell.html|archive-url = https://web.archive.org/web/20130601002059/http://incubator.apache.org/depot/version/jar-hell.html|archive-date = 2013-06-01|title = Depot - Apache Incubator}}</ref> Three ways JAR hell can occur are:▼
▲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.<ref>http://incubator.apache.org/depot/version/jar-hell.html</ref> 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
* 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"
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.
[[Java version history#Java SE 9|Java 9]] introduced the [[Java Platform Module System]] in 2017. This specifies a distribution format for collections of Java code and associated resources. It also specifies a repository for storing these collections, or ''[[Modular programming|modules]]'', and identifies how they can be discovered, loaded and checked for integrity. It includes features such as namespaces with the aim of fixing some of the shortcomings in the existing [[JAR (file format)|JAR]] format. The 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 the Java Platform Module System does not offer the ability for controlled co-existence of libraries with different versions, it does not fully address 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>
▲| accessdate=2015-11-29
==
* [[Loader (computing)]]
* [[Dynamic loading]]
Line 103 ⟶ 94:
* [[Classpath (Java)]]
* [[Java Platform Module System]]
==Footnotes==
{{reflist|group=fn}}
==References==
{{reflist
==
* Chuck McManis, "[
* 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, "[
* 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
*
* Don Schwarz, "[https://web.archive.org/web/20180506083919/http://www.onjava.com/pub/a/onjava/2005/04/13/dependencies.html
[[Category:Java (programming language)]]
|