JAR API

JAR API包括使用 manifest 文件的类。Manifest类的一个对象表示一个manifest文件。 在代码中创建一个Manifest对象,如下所示:

1
Manifest manifest =  new  Manifest();

可以从manifest文件中读取条目并向其写入条目。要将一个条目添加到主部分,使用Manifest类中的getMainAttributes()方法获取Attributes类的实例,并使用其put()方法继续向其添加名称/值对。

以下代码将一些属性添加到manifest对象的主部分。已知的属性名称在Attributes.Name类中定义为常量。

例如,常量Attributes.Name.MANIFEST_VERSION表示manifest版本属性名称。

1
2
3
4
5
Manifest manifest =  new  Manifest();
Attributes  mainAttribs = manifest.getMainAttributes(); 
mainAttribs.put(Attributes.Name.MANIFEST_VERSION,  "1.0" ); 
mainAttribs.put(Attributes.Name.MAIN_CLASS,  "cn.sxt.Main" );
  mainAttribs.put(Attributes.Name.SEALED,  "true" );

将单个条目添加到manifest文件比添加到主条目稍微复杂一点。以下代码显示如何向Manifest对象添加单个条目:

1
2
3
4
5
Map<String,Attributes> attribsMap = manifest.getEntries();
Attributes attribs  =  new  Attributes();
Attributes.Name name =  new  Attributes.Name( "Sealed" );
attribs.put(name,  "false" );
attribsMap.put( "cn/sxt/archives/" , attribs);

要将manifest文件添加到JAR文件,请在JarOutputStream类的一个构造函数中指定它。例如,以下代码创建一个jar输出流,以使用Manifest对象创建一个test.jar文件:

 

 

点击链接查看详细内容

转载于:https://www.cnblogs.com/hane/p/7306513.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值