`
messi_18
  • 浏览: 96571 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

The Extension Mechanism

阅读更多
今天决定研究一下JAR中的MAINFEST的作用。于是,看到这个文档:
http://docs.oracle.com/javase/tutorial/ext/basics/index.html
真心不错。仔细研读,写一篇总结。

Java中应用扩展有两种途径:
第一种:install extension。就是把jar包放到JRE\lib\ext目录下。这个影响是全局性的,所有被这个JRE启动的Java虚拟机进程共享这个目录。没有这么做过。jdk6增加了一种更加全局的支持:可以把jar放到一个指定的文件夹,这样就能在本地机器上所有的JRE之间共享jar包。具体设置如下:
Solaris™ Operating System: /usr/jdk/packages/lib/ext
Linux: /usr/java/packages/lib/ext
Microsoft Windows: %SystemRoot%\Sun\Java\lib\ext

第二种:download extension。就是通过MANIFEST.MF文件来指定应用需要的扩展。这里又分为两种方式:
by a Class-Path header
by an Extension-List header
每个MANIFEST文件中只能出现二者之一。有什么不同呢?第一种,只在应用的生命周期下载用到的jar;第二种会把jar放到JRE\lib\ext里,有点install extension的味道。

看完了这些资料总结成一句话:MANIFEST.MF就是为了不把所有的被依赖的jar包都放到CLASSPATH里。当然还有其他的因素,比如便于类的加载为类加载器控制,形成层次化。举个例子,A.jar中的MANIFEST.MF文件的class-path中有对B.jar的引用。如果A.jar的类是被ClassLoader1所加载,那么当需要加载B.jar中的类时,也会用ClassLoader1加载。当然,这里指的是A中的类对B中的类的直接引用,而且B还没有被ClassLoader1的parent所加载。

具体一些:如果位于A.jar中的A.class时应用程序入口,且引用了B.jar中的B.class。其中,A.jar中的MANIFEEST.MF中的Class-Path有对B.jar的引用。
启动程序java -cp A.jar A
sun.misc.Launcher$AppClassLoader的实例试图加载A.class,它把这个请求委托给了它的父加载器sun.misc.Launcher$ExtClassLoader,而ExtClassloader又把请求委托给了BootStrapClassLoader。这就达到了委托链的尽头,BootStrapClassLoader只能自己试图加载A.class,它去哪里找呢?BootStrapClassLoader去rt.jar等JRE的运行时的jar中去找。这显然是找不到的。所以它告诉它的委托人(就是sun.misc.Launcher$ExtClassLoader):我已经尽力了,没找到啊!接下来sun.misc.Launcher$ExtClassLoader只好自己动手去找,它去哪里找呢?前面提过JRE\lib\ext。这个还是找不到,于是它就只好告诉委托人(sun.misc.Launcher$AppClassLoade):我找不着!接下来,sun.misc.Launcher$AppClassLoade只好自己找,怎么着?属性java.class.path,它的默认值是.就是当前目录。这个值可以通过-cp 或者-classpath或者CLASSPATH环境变量来修改。sun.misc.Launcher$AppClassLoade回去java.class.path中的每个class,每个jar去遍历寻找。如果是class直接看是不是A.class,如果是jar就进入jar中寻找。如果还没找到,就去jar的MANIFEST.MF中Class-Path中列的jar中继续寻找。最终,在A.jar的MANIFEST.MF中定位到了B.jar,在B.jar中找到了B.class。分析到这里,我们知道了,MANIFEST.MF中的Class-Path中指定的jar起到了扩展当前ClassLoader的class path的作用。

这里举的是很简单的例子,如果在应用服务器中会更复杂一些。有时间我会尝试剖析一下JBoss、Weblogic、Websphere的类加载机制。


接下来还想搞清classloader是如何解析MANIFEST的,这个需要另开一篇。
分享到:
评论

相关推荐

    mftdma-dama.tar.gz_The Mechanism_mftdma_ns satellite_ns2 tdma_ns

    a new extension for ns2, which add the tdma-dama mechanism to the ns2

    Modeling Web Application Architecture With UML

    extension mechanism. The extension is designed so that web-specific components can be integrated with the rest of the system’s model, and to exhibit the proper level of abstraction and detail ...

    Optical_Transport_Protocol_Extensions_V1.0

    These extensions have been defined using the experimental extension mechanism supported in OpenFlow-Switch to add messages and attributes to the base protocol specification. The extensions are ...

    On the 20/40 MHz Coexistence of Overlapping BSSs in WLANs

    a 802.11n BSS operating in 20/40 MHz mode and a legacy BSS operating in 20 MHz mode, where the overlapping channel is the extension channel of the 20/40 MHz BSS. Our results show that if clear ...

    MTP Extension Overview

    MTP incorporates a mechanism to enable Microsoft and other vendors to extend the core commands, events, properties, and object formats that are defined in the MTP specification. An MTP extension is a ...

    HGX PMT dot step file

    ISO 10303-21 defines the encoding mechanism on how to represent data according to a given EXPRESS schema, but not the EXPRESS schema itself. A STEP-File is also called p21-File and STEP Physical File....

    Odoo开发手册(英文版)

    adapting existing applications to your needs through a clever extension mechanism and a very modular design. The latest versions have brought a wealth of new possibilities with the addition of a full-...

    Java邮件开发Fundamentals of the JavaMail API

    The JavaMail API is an optional package (standard extension) for reading, composing, and sending electronic messages. You use the package to create Mail User Agent (MUA) type programs, similar to ...

    development of a Bio-inspired Vision System

    In this paper, we present a new bio-inspired vision ...developed extension module and the inspired vision system are feasible to employ as a vision module for obstacle avoidance and motion control.

    acpi控制笔记本风扇转速

    it invokes the mechanism that will dynamically serialize the control method to possible prevent future errors. (BZ 440) Integrated a fix for a problem with PCI Express HID detection in the PCI Config...

    Atomistic origin of the pressure-enhanced band-gap expansion of semiconductor nanocrystals

    enhanced band-gap expansion of semiconductor nanocrystals,欧阳钢,孙长庆,We have investigated the band-gap variation of semiconductor nanocrystals based on the extension of the bond order–length-...

    The EM Algorithm and Extensions (2nd Edition)

    5. Extension of the EM Algorithm. 5.1 Introduction. 5.2 ECM Algorithm. 5.3 Multicycle ECM Algorithm. 5.4 Example 5.2: Normal Mixtures with Equal Correlations. 5.5 Example 5.3: Mixture Models for ...

    Senfore_DragDrop_v4.1

    * The Shell Extension components does not support C++ Builder 4. For some strange reason the components causes a link error. * There appear to be sporadic problems compiling with C++ Builder 5. ...

    Sample Code for GSSAPI/Kerberos v5 SASL Mechanism

    Note that the Java ...The Java Communications API is a Java extension that provides access to a variety of communication protocols and interfaces for communication between applications and devices.

    OMG 系统建模语言 (OMG SysML™) 1.6.pdf 英文版

    SysML uses the UML 2.5 extension mechanisms as further elaborated in Clause 17 as the primary mechanism to specify the extensions to UML 2.5. This revision of SysML relies on several new features ...

    H7 HAL驱动概要DM00392525_ENV2.pdf

    • Cross-family portable set of APIs covering the common peripheral features as well as extension APIs in case of specific peripheral features. • Three API programming models: polling, interrupt and ...

    java7帮助文档

    Networking Override Mechanism Security Serialization Extension Mechanism XML JAXP lang and util Base Libraries lang and util Collections Concurrency Utilities JAR Logging Management Preferences ...

    selenium-dotnet-2.45.0.zip

    SafariDriver extension into their Safari installation in order to drive the browser. This is due to Apple's changes in architecture for Safari extensions. * Added initial implementation of .NET ...

    RFC4244.An_Extension_to_SIP_for_Request_History_Information

    This document defines a standard mechanism for capturing the history information associated with a Session Initiation Protocol (SIP) request. This capability enables many enhanced services by ...

Global site tag (gtag.js) - Google Analytics