JAR API
JAR API包括使用 manifest 文件的类。Manifest类的一个对象表示一个manifest文件。 在代码中创建一个Manifest对象,如下所示:
Manifest manifest = new Manifest();
可以从manifest文件中读取条目并向其写入条目。要将一个条目添加到主部分,使用Manifest类中的getMainAttributes()方法获取Attributes类的实例,并使用其put()方法继续向其添加名称/值对。
以下代码将一些属性添加到manifest对象的主部分。已知的属性名称在Attributes.Name类中定义为常量。
例如,常量Attributes.Name.MANIFEST_VERSION表示manifest版本属性名称。
Manifest manifest = new Manifest();
Attributes mainAttribs = manifest.getMainAttributes(); mainAttribs.put(Attributes.Name.MANIFEST_VERSION, "1.0"); mainAttribs.put(Attributes.Name.MAIN_CLASS, "com.yiibai.Main"); mainAttribs.put(Attributes.Name.SEALED, "true");
将单个条目添加到manifest文件比添加到主条目稍微复杂一点。以下代码显示如何向Manifest对象添加单个条目:
Map attribsMap = manifest.getEntries();
Attributes attribs = new Attributes();
Attributes.Name name = new Attributes.Name("Sealed");
attribs.put(name, "false");
attribsMap.put("com/yiibai/archives/", attribs);
要将manifest文件添加到JAR文件,请在JarOutputStream类的一个构造函数中指定它。例如,以下代码创建一个jar输出流,以使用Manifest对象创建一个test.jar文件:
Manifest manifest = new Manifest();
JarOutputStream jos = new JarOutputStream(new BufferedOutputStream(
new FileOutputStream("test.jar")), manifest);
以下代码创建包含Manifest文件的JAR文件。
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import java.util.zip.Deflater;
public class Main {
public static void main(String[] args) throws Exception {
Manifest manifest = getManifest();
String jarFileName = "jartest.jar";
String[] entries = new String[2];
entries[0] = "images/logo.bmp";
entries[1] = "com/yiibai/Test.class";
createJAR(jarFileName, entries, manifest);
}
public static void createJAR(String jarFileName, String[] jarEntries,
Manifest manifest) {
try (JarOutputStream jos = new JarOutputStream(new BufferedOutputStream(
new FileOutputStream(jarFileName)), manifest)) {
jos.setLevel(Deflater.BEST_COMPRESSION);
for (int i = 0; i < jarEntries.length; i++) {
File entryFile = new File(jarEntries[i]);
if (!entryFile.exists()) {
return;
}
JarEntry je = new JarEntry(jarEntries[i]);
jos.putNextEntry(je);
addEntryContent(jos, jarEntries[i]);
jos.closeEntry();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void addEntryContent(JarOutputStream jos, String entryFileName)
throws IOException, FileNotFoundException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
entryFileName));
byte[] buffer = new byte[1024];
int count = -1;
while ((count = bis.read(buffer)) != -1) {
jos.write(buffer, 0, count);
}
bis.close();
}
public static Manifest getManifest() {
Manifest manifest = new Manifest();
Attributes mainAttribs = manifest.getMainAttributes();
mainAttribs.put(Attributes.Name.MANIFEST_VERSION, "1.0");
mainAttribs.put(Attributes.Name.MAIN_CLASS, "com.yiibai.Test");
mainAttribs.put(Attributes.Name.SEALED, "true");
Map attribsMap = manifest.getEntries();
Attributes a1 = getAttribute("Sealed", "false");
attribsMap.put("com/yiibai/", a1);
Attributes a2 = getAttribute("Content-Type", "image/bmp");
attribsMap.put("images/logo.bmp", a2);
return manifest;
}
public static Attributes getAttribute(String name, String value) {
Attributes a = new Attributes();
Attributes.Name attribName = new Attributes.Name(name);
a.put(attribName, value);
return a;
}
}
要从JAR文件的清单文件读取条目,请使用JarInputStream的getManifest()类获取Manifest类的对象,如下所示:
JarInputStream jis = new JarInputStream(new FileInputStream("jartest.jar"));
Manifest manifest = jis.getManifest();
if (manifest != null) {
Attributes mainAttributes = manifest.getMainAttributes();
String mainClass = mainAttributes.getValue("Main-Class");
Map entries = manifest.getEntries();
}
从JAR文件访问资源
可以通过使用JAR文件中的资源引用来构造URL对象。JAR文件URL语法为 -
jar:!/{entry}
以下URL使用HTTP协议引用www.yiibai.com上的test.jar文件中的images/logo.bmp JAR条目如下:
jar:http://www.yiibai.com/test.jar!/images/logo.bmp
以下URL使用文件协议引用c:\jarfiles\ 目录中的本地文件系统上的test.jar文件中的images/logo.bmp JAR条目:
jar:file:/c:/jarfiles/test.jar!/images/logo.bmp
要从类路径中的JAR文件读取images/logo.bmp文件,可以使用类对象获取输入流对象,如下所示:
// Assuming that the Test class is in the CLASSPATH
Class cls = Test.class;
InputStream in = cls.getResourceAsStream("/images/logo.bmp")
还可以在JAR文件中获取一个条目的URL对象,该路径在类路径中如下所示:
URL url = cls.getResource("/images/logo.bmp");
¥ 我要打赏
纠错/补充
收藏
加QQ群啦,易百教程官方技术学习群
注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。
这篇博客介绍了如何在Java中使用Manifest类操作JAR文件的清单,包括创建、添加条目、读取和写入属性。示例代码展示了如何创建、写入和读取JAR文件的Manifest,以及如何通过URL访问JAR资源。
1万+

被折叠的 条评论
为什么被折叠?



