當我準備用於部署的程序時,我將它打包到JAR中,以及Eclipse jar jar-in-jar類裝入器。當我的程序從JAR運行時,我需要知道程序包的版本,但我無法以簡單和「誠實」的方式從jar的清單中獲得它。 清單如下:如何使用Package類的getImplementationVersion()方法從JAR的清單中獲取包版本
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
爲了獲得包的實現版本,我嘗試用最簡單和直接的方式:
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類進行解析,但是我想了解使用方法的正確方法是什麼。
這有一個答案[here](http://stackoverflow.com/questions/33553570/how-to-read-meta-data-from-manifest-file)已經 – SomeDude
是的,我敢肯定。我在JAR中看到了它在正確的位置(.META-INF /)贏得了一個開放的目光,我可以通過編程方式閱讀它(包括Implementaion-Version)。我只是不想加載清單並解析它,當getImplementationVersion()存在時。 –
@svasa,這不是我的問題的答案。我沒有問清楚如何閱讀清單。我的問題是「如何使用getImplementationVersion()方法」。我應該更清楚地表達這個問題。我會編輯它。 –