Manifesto

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 or 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

<!--
    Ant build file for Manifesto

    Targets suitable for NetBeans free-form project.

    http://ant.apache.org/
-->

<project name="Manifesto" default="default" basedir=".">

<!-- set global properties for this build -->
<property name="proj" value="Manifesto"/>
<property name="main" value="Manifesto"/>
<property name="srcDir" value="."/>
<property name="buildDir" value="."/>
<property name="distDir" value="."/>
<property name="tempDir" value="temp"/>
<property name="javadocDir" value="javadoc"/>
<property name="jversion" value="1.5"/>
<property name="version" value="1.0.0"/>

<!-- Initialize -->
<target name="init" description="Initialize the build process.">
    <!-- Create a time stamp -->
    <tstamp/>
</target>

<!-- Compile source files to class files -->
<target name="compile" depends="init" description="Compile the program.">
    <mkdir dir="${buildDir}"/>
    <javac srcDir="${srcDir}" destDir="${buildDir}"
        debug="on" source="${jversion}" target="${jversion}">
        <compilerarg value="-Xlint"/>
    </javac>
</target>

<!-- Build jar from classes -->
<target name="jar" depends="compile" description="Build a jar file.">
    <jar destfile="${distDir}/${proj}.jar">
        <manifest>
            <attribute name="Main-Class" value="${main}"/>
        </manifest>
        <fileset file="build.xml"/>
        <fileset file="${srcDir}/${main}.java"/>
        <fileset file="${buildDir}/${main}.class"/>
    </jar>
</target>

<!-- Run the program -->
<target name="run" depends="jar" description="Run the program.">
    <java jar="${distDir}/${proj}.jar" fork="true"></java> 
</target>

<!-- Run the program -->
<target name="test" depends="run" description="Test the program.">
    <java jar="${distDir}/${proj}.jar" fork="true">
    	<arg line="${proj}.jar"/>
    </java> 
</target>

<!-- Clean up all build/dist/temp directories -->
<target name="clean" description="Clean the project.">
    <delete dir="${javadocDir}"/>
</target>

<!-- Generate javadoc HTML documentation -->
<target name="doc" depends="compile" description="Generate the javadoc.">
    <mkdir dir="${javadocDir}"/>
    <javadoc
        sourcepath="${srcDir}"
        classpath="${buildDir}"
        destdir="${javadocDir}"
        author="true" version="true"
        access="private" use="true"
        windowtitle="${proj} v${version} API">
        <fileset dir="${srcDir}">
            <include name="**/*.java"/>
        </fileset>
        <doctitle>
            <![CDATA[<h2>${proj} v${version} API</h2>]]>
        </doctitle>
        <bottom>
            <![CDATA[<i>Copyright &#169; 2008 Gem City Software.]]>
        </bottom>
    </javadoc>
</target>

<!-- NetBeans debug target -->
<target  name="debug-nb" depends="compile" description="Debug the program in NetBeans.">
    <!-- Run the program -->
    <nbjpdastart addressproperty="jpda.address" name="${main}" transport="dt_socket">
        <classpath path="${buildDir}"/>
    </nbjpdastart>
    <java classname="${main}" fork="true">
        <classpath path="${buildDir}"/>
        <jvmarg value="-Xdebug"/>
        <jvmarg value="-Xnoagent"/>
        <jvmarg value="-Djava.compiler=none"/>
        <jvmarg value="-Xrunjdwp:transport=dt_socket,address=${jpda.address}"/>
    </java>
</target>

<!-- Default target -->
<target name="default" depends="clean, jar" description="Default clean and build.">
</target>

</project>


Č
ċ
ď
Manifesto.jar
(3k)
John Matthews,
Nov 14, 2010, 3:28 PM
Comments