如何使用 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/教程/deployment/jar/packageman.html 和 http://docs.oracle.com/javase/8/docs/api/java/lang/Package.html#getImplementationVersion--(和其他来源),它应该返回19",但它返回空值.对于 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 中读取 manifest 文件并获取您需要的值,如下所示:
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屋!