Java programs are often deployed in JAR format, a specialized kind of zip file. The manifest is a text file inside the JAR that describes the contents. Here's a simple program to read the manifest from one of more JAR files. You can read more about JAR files in the JAR tutorial and technotes. The attached JAR file, below, includes the source and an ant build script.
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
/**
* Display the manifest of a jar file.
* @see <a href="http://java.sun.com/docs/books/tutorial/deployment/jar">JAR tutorial</a>
* @author John B. Matthews
*/
public class Manifesto {
public static void main(String[] args) {
if (args.length < 1) showHelp();
else for (String name : args)
try {
System.out.println("Manifest: " + name);
JarFile jar = new JarFile(name);
Manifest manifest = jar.getManifest();
showMap(manifest.getMainAttributes());
} catch (Exception e) {
System.err.println(e.getMessage());
showHelp();
System.exit(1);
}
}
private static void showMap(Attributes map) {
if (map == null) return;
for (Map.Entry<Object, Object> e : map.entrySet())
System.out.println(e.getKey() + ": " + e.getValue());
}
private static void showHelp() {
System.out.println(
"Usage: java -jar Manifesto.jar <jarfile> [<jarfile>]");
}
}
$ java -jar Manifesto.jar Manifesto.jar
Manifest: Manifesto.jar
Created-By: 1.5.0_16-133 (Apple Inc.)
Ant-Version: Apache Ant 1.7.0
Main-Class: Manifesto
Manifest-Version: 1.0
|
|