最近在看Hadoop的源码,发现在用 hadoop jar *.jar 这个命令的时候调用RunJar这个类,在这个类里面用到了Manifest这个东西,于是乎学习了一下,只是简单的学习了一下。
先说下jar文件,也就是jar包实际上就是一个zip压缩文件(为什么是zip,下文介绍),这个压缩文件里面就是java的class文件,此外每一个jar包里面都有一个名为META-INF的文件夹,在这个文件夹里面有一个名为MANIFEST.MF的文件,这个文件实际上保存的就是这个jar包的一些标签信息,当运行一个jar文件时,会首先去读取这个文件获得class文件执行的一些信息。MANIFEST.MF形如:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.3
Created-By: 1.6.0_65-b14-462-11M4609 (Apple Inc.)
Main-Class: org.apache.tools.ant.Main
Name: org/apache/tools/ant/
Extension-name: org.apache.tools.ant
Specification-Title: Apache Ant
Specification-Version: 1.9.3
Specification-Vendor: Apache Software Foundation
Implementation-Title: org.apache.tools.ant
Implementation-Version: 1.9.3
Implementation-Vendor: Apache Software Foundation如上所示,都是以“属性: 值”的形式保存的,这些信息里面最重要的是Main-Class这个属性,因为在执行这个jar文件的时候,jdk需要一个程序执行的入口类(或者叫主类),找到这个类之后,才开始执行这个jar包。
下面来说说到底是怎么找这个入口类的。
在jdk的java.util.jar这个包里的JarFile这个类,这个类继承自ZipFile这个类,其实通过继承的关系就可以知道上文提及的jar包就是zip压缩文件这个问题。
JarFile这个类有5个重载的构造方法,不过其中4个都是调用的JarFile(File file, boolean verify, int mode)这个方法,代码如下:
/**
* Creates a new <code>JarFile</code> to read from the specified
* <code>File</code> object in the specified mode. The mode argument
* must be either <tt>OPEN_READ</tt> or <tt>OPEN_READ | OPEN_DELETE</tt>.
*
* @param file the jar file to be opened for reading
* @param verify whether or not to verify the jar file if
* it is signed.
* @param mode the mode in which the file is to be opened
* @throws IOException if an I/O error has occurred
* @throws IllegalArgumentException
* if the <tt>mode</tt> argument is invalid
* @throws SecurityException if access to the file is denied
* by the SecurityManager
* @since 1.3
*/
public JarFile(File file, boolean verify, int mode) throws IOException {
super(file, mode);
this.verify = verify;
}这个方法有三个参数:
file:自然就是jar文件了
verify:标识jar文件是否需要验证
mode:打开文件的模式,只有两种方式:OPEN_READ表示打开,OPEN_DELETE表示打开后,并在关闭jarfile之前删除
实际上最终调用的是其父类的构造方法ZipFile(File file, int mode),这个还没仔细看,以后再说吧。
下面看JarFile的getManifest()这个方法,就是通过这个方法来得到jar文件的MANIFEST的,代码:
/**
* Returns the jar file manifest, or <code>null</code> if none.
*
* @return the jar file manifest, or <code>null</code> if none
*
* @throws IllegalStateException
* may be thrown if the jar file has been closed
*/
public Manifest getManifest() throws IOException {
return getManifestFromReference();
}
private Manifest getManifestFromReference() throws IOException {
Manifest man = manRef != null ? manRef.get() : null;
if (man == null) {
JarEntry manEntry = getManEntry();
// If found then load the manifest
if (manEntry != null) {
if (verify) {
byte[] b = getBytes(manEntry);
man = new Manifest(new ByteArrayInputStream(b));
if (!jvInitialized) {
jv = new JarVerifier(b);
}
} else {
man = new Manifest(super.getInputStream(manEntry));
}
manRef = new SoftReference(man);
}
}
return man;
}getManifest()这个方法调用getManifestReference()这个方法,返回一个Manifest对象,这个对象里面就保存了MANIFEST.MF这个文件里面的信息。下面看getManifestReference这个方法,这个方法里首先看manRef(JarFile的成员变量,软引用类的对象,声明为Manifest类的软引用,至于为啥用软引用,这个还没搞明白)这个变量是否为空,若不为空则通过get方法返回一个Manifest的引用给man这个变量(有点绕啊)。当man为空时,声明一个JarEntry类型的变量manEntry(这个变量就代表了jar文件的入口),通过getManEntry这个方法获取入口,下面为getManEntry的代码:
private JarEntry getManEntry() {
if (manEntry == null) {
// First look up manifest entry using standard name
manEntry = getJarEntry(MANIFEST_NAME);
if (manEntry == null) {
// If not found, then iterate through all the "META-INF/"
// entries to find a match.
String[] names = getMetaInfEntryNames();
if (names != null) {
for (int i = 0; i < names.length; i++) {
if (MANIFEST_NAME.equals(
names[i].toUpperCase(Locale.ENGLISH))) {
manEntry = getJarEntry(names[i]);
break;
}
}
}
}
}
return manEntry;
}getJarEntry这个方法的参数MANIFEST_NAME的值为:public static final String MANIFEST_NAME = "META-INF/MANIFEST.MF"; 这个文件终于出现了。。。
目前就看了这么多,未完待续。。。
本文介绍了jar包中的Manifest.MF文件的作用,它存储了jar包的元信息,如入口类等。当运行jar文件时,系统会读取MANIFEST.MF以获取执行信息。文中还简单探讨了JarFile类如何读取并验证MANIFEST.MF。
2352





