|
|
A java program that reads the manifest of a jar file |
Today, a colleague asked me if I had an example of a
Java program that would read the manifest of a jar file.
Since I hadn't any, I created one. Here it is. It's very simple,
just a few lines of code...
I'm posting it here hoping that it can help someone else!
Update: And if you haven't read it yet - there's
also an interesting side story :-)
Cheers,
--daniel
package readmanifest;
import java.io.File;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.Attributes.Name;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.logging.Logger;
@author
public class ReadManifest {
private static final Logger LOG =
Logger.getLogger(ReadManifest.class.getName());
@paramargs
public static void main(String[] args) {
for (String arg : args) {
final File f = new File(arg);
if (!f.canRead()) {
System.err.println(f.getAbsolutePath()+": no such file");
continue;
}
try {
JarFile jar = new JarFile(f);
final Manifest manifest = jar.getManifest();
final Attributes mattr = manifest.getMainAttributes();
System.out.println(f.getAbsolutePath());
System.out.println("Main attrs: ");
for (Object a : mattr.keySet()) {
System.out.println("\t "+a+": "+mattr.getValue((Name)a));
}
System.out.println("\nReading other attrs:\n");
final Map<String,Attributes> attrs = manifest.getEntries();
for (String name : attrs.keySet()) {
final Attributes attr = attrs.get(name);
System.out.println(name+": \n");
for (Object a : attr.keySet()) {
System.out.println("\t "+a+": "+attr.getValue((Name)a));
}
}
} catch (Exception x) {
System.err.println("Failed to read manifest for "+
f.getAbsolutePath()+": "+x);
}
}
}
}
|
Tags:
example
jar
java
manifest
Posted by dfuchs
( Jun 20 2007, 03:01:33 PM CEST )
Permalink
|
Posted by Emilian Bold on June 20, 2007 at 04:06 PM CEST #
Posted by daniel on June 20, 2007 at 05:28 PM CEST #
Posted by Andy Schwarz on June 21, 2007 at 03:56 PM CEST #
I am studying SCJP now. I
Posted by Roshean on October 10, 2007 at 06:28 PM CEST #