Is there any way i can read the contents of a jar file. like i want to read the manifest file in order to find the creator of the jar file and version. Is there any way to achieve the same.
A:
You could use something like this:
public static String getManifestInfo() {
Enumeration resEnum;
try {
resEnum = Thread.currentThread().getContextClassLoader().getResources(JarFile.MANIFEST_NAME);
while (resEnum.hasMoreElements()) {
try {
URL url = (URL)resEnum.nextElement();
InputStream is = url.openStream();
if (is != null) {
Manifest manifest = new Manifest(is);
Attributes mainAttribs = manifest.getMainAttributes();
String version = mainAttribs.getValue("Implementation-Version");
if(version != null) {
return version;
}
}
}
catch (Exception e) {
// Silently ignore wrong manifests on classpath?
}
}
} catch (IOException e1) {
// Silently ignore wrong manifests on classpath?
}
return null;
}
To get the manifest attributes, you could iterate over the variable "mainAttribs" or directly retrieve your required attribute if you know the key.
flash
2010-09-23 09:46:05
+1
A:
Next code should help:
JarInputStream jarStream = new JarInputStream(stream);
Manifest mf = jarStream.getManifest();
Exception handling is left for you :)
iYasha
2010-09-23 09:46:10