Java类加载器
Java Classloader是Java运行时环境(Java Runtime Environment)的一部分,负责动态加载Java类到Java虚拟机的内存空间中。[1]类通常是按需加载,即第一次使用该类时才加载。由于有了类加载器,Java运行时系统不需要知道文件与文件系统。学习类加载器时,掌握Java的委派概念很重要。
每个Java类必须由某个类加载器装入到内存。[2]Java程序可以利用外部库(即由其他作者编写的软件库)。
- 引导(Bootstrap)类加载器。由原生代码(如C语言)编写,不继承自
java.lang.ClassLoader
。负责加载核心Java库[5],存储在<JAVA_HOME>/jre/lib
目录中。 - 扩展(Extensions)类加载器。用来在
<JAVA_HOME>/jre/lib/ext
,[6]或java.ext.dirs
中指明的目录中加载 Java的扩展库。Java 虚拟机的实现会提供一个扩展库目录。该类加载器在此目录里面查找并加载 Java 类。该类由sun.misc.Launcher$ExtClassLoader
实现。 - Apps类加载器(也称系统类加载器)。根据 Java应用程序的类路径(
java.class.path
或CLASSPATH环境变量)来加载 Java 类。一般来说,Java 应用的类都是由它来完成加载的。可以通过 ClassLoader.getSystemClassLoader()来获取它。该类由sun.misc.Launcher$AppClassLoader
实现。
每个类装载器有一个父装载器(parent class loader)。
User-defined class loaders
可以通过继承java.lang.ClassLoader
类的方式实现自己的类加载器,以满足一些特殊的需求而不需要完全了解Java虚拟机的类加载的细节。
可用于:
- 运行时装载或卸载类。这常用于:
- 改变Java字节码的装入,例如,可用于Java类字节码的加密装入。[7]).
- 修改已装入的字节码weaving of aspects when using aspect-oriented programming).
JEE的类装载
Java EE (JEE)应用程序服务器典型地用树状的一组类装载器从已部署的WAR或EAR文档中装入类。这使得应用程序之间彼此隔离,但共享已部署模块。servlet container一般被实现为多个类装载器。[2][8]
JAR地狱
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:
- 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.
- 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.[10]
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, 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]
参考文献
- ^ Mcmanis, Chuck. The basics of Java class loaders. JavaWorld. 1996-10-01 [2008-01-26].
- ^ 2.0 2.1 Christudas, Binildas. Internals of Java Class Loading. onjava.com. 2005-01-26 [2009-10-02].
- ^ Understanding Extension Class Loading. java.sun.com. 2008-02-14 [2009-12-08].
- ^ Sosnoski, Dennis. Classes and class loading. ibm.com. 2003-04-29 [2008-01-26].
- ^ 存储在Jar文件中,如rt.jar, core.jar, server.jar等。
- ^ http://docs.oracle.com/javase/tutorial/ext/basics/load.html
- ^ Roubtsov, Vladimir. Cracking Java byte-code encryption. javaworld.com. 2003-05-09 [2008-01-26].
- ^ deBoer, Tim; Karasiuk, Gary. J2EE Class Loading Demystified. ibm.com. 2002-08-21 [2008-01-26].
- ^ http://incubator.apache.org/depot/version/jar-hell.html
- ^ http://articles.qos.ch/classloader.html
- ^ http://www.osgi.org/News/20081217
外部链接
- Chuck McManis, "The basics of Java class loaders", 1996
- Brandon E. Taylor, "Java Class Loading: The Basics", 2003
- Jeff Hanson, "Take Control of Class Loading in Java", 2006-06-01
- Andreas Schaefer, "Inside Class Loaders", 2003-11-12
- Sheng Liang and Gilad Bracha, "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. 36–44
- Dr. Christoph G. Jung, "Classloaders Revisited Hotdeploy", Java Specialist Newsletter, 2001-06-07
- Don Schwarz, "Managing Component Dependencies Using ClassLoaders", 2005-04-13