我想知道是否可以在运行时检索该类来自的jar的版本号?
我知道有可能找到该类来自的jar:
MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();
但是版本呢?
(假设其不在文件名中:)
最佳答案
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);
}
}
关于java - 在运行时获取jar版本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21724145/