跳转到内容

Java类加载器

维基百科,自由的百科全书

这是本页的一个历史版本,由Newmangling留言 | 贡献2014年5月14日 (三) 23:09 建立内容为“'''Java Classloader'''是Java运行时环境(Java Runtime Environment)的一部分,负责动态加载Java类Java虚拟机...”的新页面)编辑。这可能和当前版本存在着巨大的差异。

(差异) ←上一修订 | 最后版本 (差异) | 下一修订→ (差异)

Java ClassloaderJava运行时环境(Java Runtime Environment)的一部分,负责动态加载Java类Java虚拟机的内存空间中。[1]类通常是按需加载,即第一次使用该类时才加载。由于有了类加载器,Java运行时系统不需要知道文件与文件系统。学习类加载器时,掌握Java的委派概念很重要。

每个Java类必须由某个类加载器装入到内存。[2]Java程序可以利用外部库(即由其他作者编写的软件库)。

JVM中有3个默认的类加载器:[3][4]

  1. 引导(Bootstrap)类加载器。由原生代码(如C语言)编写,不继承自java.lang.ClassLoader。负责加载核心Java库[5],存储在<JAVA_HOME>/jre/lib目录中。
  2. 扩展(Extensions)类加载器。用来在<JAVA_HOME>/jre/lib/ext,[6]java.ext.dirs中指明的目录中加载 Java的扩展库。Java 虚拟机的实现会提供一个扩展库目录。该类加载器在此目录里面查找并加载 Java 类。该类由sun.misc.Launcher$ExtClassLoader实现。
  3. 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虚拟机的类加载的细节。

可用于:

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]

参考文献

  1. ^ Mcmanis, Chuck. The basics of Java class loaders. JavaWorld. 1996-10-01 [2008-01-26]. 
  2. ^ 2.0 2.1 Christudas, Binildas. Internals of Java Class Loading. onjava.com. 2005-01-26 [2009-10-02]. 
  3. ^ Understanding Extension Class Loading. java.sun.com. 2008-02-14 [2009-12-08]. 
  4. ^ Sosnoski, Dennis. Classes and class loading. ibm.com. 2003-04-29 [2008-01-26]. 
  5. ^ 存储在Jar文件中,如rt.jar, core.jar, server.jar等。
  6. ^ http://docs.oracle.com/javase/tutorial/ext/basics/load.html
  7. ^ Roubtsov, Vladimir. Cracking Java byte-code encryption. javaworld.com. 2003-05-09 [2008-01-26]. 
  8. ^ deBoer, Tim; Karasiuk, Gary. J2EE Class Loading Demystified. ibm.com. 2002-08-21 [2008-01-26]. 
  9. ^ http://incubator.apache.org/depot/version/jar-hell.html
  10. ^ http://articles.qos.ch/classloader.html
  11. ^ http://www.osgi.org/News/20081217

外部链接