如何使用Package类的getImplementationVersion()方法从JAR的清单中获取包版本 [英] How to obtain a package version from the JAR's manifest, using the getImplementationVersion() method of the Package class
问题描述
当我准备我的程序进行部署时,我将它与Eclipse jar-in-jar类加载器一起打包到JAR中。当我的程序从JAR运行时,我需要知道一个包的版本,但我不能从jar的清单中以简单和诚实的方式获取它。
清单如下所示:
When I prepare my program for deployment, I pack it into a JAR, along with the Eclipse jar-in-jar class loader. When my program runs from the JAR, I need to know a package's version, but I can not obtain it from the jar's manifest a simple and "honest" way. The manifest looks like this:
Manifest-Version: 1.0
Created-By: 1.8.0_73-b02 (Oracle Corporation)
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
Rsrc-Main-Class: com.domain.sp2.controller.Controller
Rsrc-Class-Path: ./ jar-in-jar-loader.zip javahelp-2.0.05.jar json-simple-1.1.1.jar
Class-Path: .
Name: com/domain/sp2/controller/
Implementation-Version: 19
要获得软件包的实现版本,我尝试使用最简单直接的方法:
To obtain the package's implementation version, I try to use the simplest and straight-forward way:
package com.domain.sp2.controller;
public class Controller {
...
public static String getBuildNumber() throws IOException {
Package pckg = Controller.class.getPackage();
pr(pckg.getName()); // prints "com.domain.sp2.controller", as expected
return pckg.getImplementationVersion(); // returns null
}
...
}
根据 http://docs.oracle.com/javase/tutorial/ deployment / jar / packageman.html 和 http://docs.oracle.com/javase/8/docs/api/java/lang/Package.html#getImplementationVersion-- (和其他来源),它应该返回19,但它返回null。对于JRE库的包,它返回正确的值。也许我错过了关于如何在清单中命名包的详细信息,或者与 JarRsrcLoader
相关的原因 - 可能需要一些特殊的语法来解决包。我也试过。com / domain /...\"
,/ com / domain /...\"
和... / controller
,甚至rsrc:./ com / domain ...
作为清单中的包名称 - 一切都没有成功。我可以使用其他方式,例如将清单加载为流并使用Manifest类解析它,但我想了解使用 getImplementationVersion()
方法的正确方法。
According to http://docs.oracle.com/javase/tutorial/deployment/jar/packageman.html and http://docs.oracle.com/javase/8/docs/api/java/lang/Package.html#getImplementationVersion-- (and other sources), it should return "19", but it returns null. For the packages of the JRE libraries, it returns correct values. Perhaps I have missed a detail about how to name the package in the manifest, or the reason pertains to the JarRsrcLoader
- may be it requires some special syntax to address packages. I have also tried ".com/domain/..."
, "/com/domain/..."
and ".../controller"
, and even "rsrc:./com/domain..."
as the package name in the manifest - all without success. I could use other ways, e.g. to load the manifest as stream and parse it with the Manifest class, yet I would like to understand what is the correct way to use the getImplementationVersion()
method.
推荐答案
您可以从classLoader中读取清单文件,并获得您需要的值,如下所示:
You can read the manifest file from the classLoader and get the value you need as below:
URLClassLoader cl = (URLClassLoader) YOUR_CLASS.class.getClassLoader();
try {
URL url = cl.findResource("META-INF/MANIFEST.MF");
Manifest manifest = new Manifest(url.openStream());
Attributes mainAttributes = manifest.getMainAttributes();
String implVersion = mainAttributes.getValue("Implementation-Version");
System.out.println(implVersion);
} catch (IOException E) {
// handle
}
这篇关于如何使用Package类的getImplementationVersion()方法从JAR的清单中获取包版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!