Wednesday, April 4, 2012

Reading MANIFEST.MF file in java

You can read any attribute of MANIFEST.MF file. The below example show you to read the attribute for any JAR file in the CLASSPATH , here I am trying to read the Version number.



public String getVersion() {
String version=null;
try{
Enumeration <URL> resources =getClass().getClassLoader().getResources("META-INF/MANIFEST.MF");
while(resources.hasMoreElements()){
URL url = resources.nextElement();
if(StringUtils.contains(url.getFile(), "fileName")){
Manifest manifest = new Manifest(url.openStream());
version =manifest.getMainAttributes().getValue("Implementation-Version");
break;
}
}
}catch(Exception ex){
log.error("ERROR while readign the Version "+ ex.getLocalizedMessage());
}
return version;
}

No comments:

Post a Comment