在运行时获取jar版本 [英] Get jar version in runtime
本文介绍了在运行时获取jar版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否有可能在运行时检索来自该类的jar的版本号?
I'm wondering if it is possible to retrieve, in runtime, a version number of a jar from which the class comes from?
我知道它可以找到罐子从中类来自:。
I know its possible to find jar from which the class comes from:
MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();
但版本怎么样?
(假设它不在文件名中:))
(assuming its not in the file name:) )
推荐答案
import javax.mail.internet.InternetAddress;
/**
Display package name and version information for javax.mail.internet.
*/
public final class ReadVersion {
public static void main(String... aArgs){
ReadVersion readVersion = new ReadVersion();
readVersion.readVersionInfoInManifest();
}
public void readVersionInfoInManifest(){
InternetAddress object = new InternetAddress();
Package objPackage = object.getClass().getPackage();
//examine the package object
String name = objPackage.getSpecificationTitle();
String version = objPackage.getSpecificationVersion();
//some jars may use 'Implementation Version' entries in the manifest instead
System.out.println("Package name: " + name);
System.out.println("Package version: " + version);
}
}
这篇关于在运行时获取jar版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文