의 패키지 클래스 java.lang.Package에는 원하는 것을 수행하는 방법이 있습니다.
String t = this.getClass().getPackage().getImplementationTitle();
String v = this.getClass().getPackage().getImplementationVersion();
내가 공유 유틸리티 class.The 방법에 정적 메서드에이를 넣어 매개 변수로 클래스 핸들 객체는 받아 들인다 : 여기에 귀하의 자바 코드를 사용하여 매니페스트 내용을 얻을 수있는 가장 쉬운 방법입니다. 이렇게하면 시스템의 모든 클래스가 필요할 때마다 자체적 인 정보를 얻을 수 있습니다. 당연히 메소드는 값의 배열 또는 해시 맵을 반환하도록 쉽게 수정 될 수 있습니다.
호출 방법 :
String ver = GeneralUtils.checkImplVersion(this);
GeneralUtils.java라는 파일의 방법
public static String checkImplVersion(Object classHandle)
{
String v = classHandle.getClass().getPackage().getImplementationVersion();
return v;
}
그리고 당신이 패키지를 통해 얻을 수있는 것 이외의 매니페스트 필드 - 값을 얻을 수 클래스 (예 : 자신의 빌드 - 날짜), 당신은 주요 Attibutes를 얻고 그들을 통해 일하고, 당신이 원하는 특정 것을 요구합니다. 이 다음 코드는 내가 찾은 유사한 질문의 약간의 모드입니다. 아마도 여기에 있습니다. (나는 그것을 신용하고 싶지만 미안하다.)
이것을 try-catch 블록에 넣고 classHandle ("this"또는 MyClass.class)을 메서드에 전달한다. "classHandle는"Class 형이다 :
String buildDateToReturn = null;
try
{
String path = classHandle.getProtectionDomain().getCodeSource().getLocation().getPath();
JarFile jar = new JarFile(path); // or can give a File handle
Manifest mf = jar.getManifest();
final Attributes mattr = mf.getMainAttributes();
LOGGER.trace(" --- getBuildDate: "
+"\n\t path: "+ path
+"\n\t jar: "+ jar.getName()
+"\n\t manifest: "+ mf.getClass().getSimpleName()
);
for (Object key : mattr.keySet())
{
String val = mattr.getValue((Name)key);
if (key != null && (key.toString()).contains("Build-Date"))
{
buildDateToReturn = val;
}
}
}
catch (IOException e)
{ ... }
return buildDateToReturn;
의 가능한 중복 http://stackoverflow.com/questions/2198525/can-values-defined-in-manifest-mf-be-accessed-programmatically/2198542 –