欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

给Jar包添加版本信息,Jar包添加版本信息,我们有下面的包代码:pa

来源: javaer 分享于  点击 18074 次 点评:270

给Jar包添加版本信息,Jar包添加版本信息,我们有下面的包代码:pa


我们有下面的包代码:

package cn.outofmemory;public class Hello { public static void main(String[] args) {    new Hello().say("Hello World!");    } public void say(String s) {    System.out.println(s); }}

要给jar包添加版本信息,需要先创建 MANIFEST.MF 文件 :

Manifest-Version: 1.0Main-Class: cn.outofmemory.Hello

编译执行jar包:

>jar cvfm hello.jar MANIFEST.MF cn\outofmemory\Hello.class>java -jar hello.jarHello World!

修改manifest 文件来包含版本信息:

Manifest-Version: 1.0Main-Class: com.rgagnon.HelloSpecification-Version: 2.1Implementation-Version: 1.1

修改Hello类来显示版本信息

package cn.outofmemory;public class Hello { public static void main(String[] args) {    new Hello().say("Hello World!");    } Hello() {   Package p = this.getClass().getPackage();   System.out.println("Hello Specification Version : "                          + p.getSpecificationVersion());   System.out.println("Hello Implementation Version : "                          + p.getImplementationVersion()); } public void say(String s) {    System.out.println(s); }}

编译并执行Hello

> jar cvfm hello.jar MANIFEST.MF cn\outofmemory\Hello.class> java -jar hello.jarSpecification Version : 2.1Implementation Version : 1.1Hello World!

下面的例子,打开一个jar包并修改其版本信息:

package cn.outofmemory.howto;import java.util.jar.*;public class JarUtils {  public static String getJarImplementationVersion(String jar)       throws java.io.IOException  {    JarFile jarfile = new JarFile(jar);    Manifest manifest = jarfile.getManifest();    Attributes att = manifest.getMainAttributes();    return att.getValue("Implementation-Version");  }  public static String getJarSpecificationVersion(String jar)       throws java.io.IOException  {    JarFile jarfile = new JarFile(jar);    Manifest manifest = jarfile.getManifest();    Attributes att = manifest.getMainAttributes();    return att.getValue("Specification-Version");  }  public static void main(String[] args) throws java.io.IOException{    String javaMailJar = "C:/Program Files/Java/jre1.5.0/lib/mail.jar";    System.out.println("Specification-Version: "         + JarUtils.getJarSpecificationVersion(javaMailJar));    System.out.println("Implementation-Version: "         + JarUtils.getJarImplementationVersion(javaMailJar));    /*     * output :     *      Specification-Version: 1.3     *      Implementation-Version: 1.3.1     */  }}
相关栏目:

用户点评