Modifying a Manifest File
- You can modify the contents of a JAR file in 2 ways:
- Use
the m option to add custom information to the
manifest during creation of a JAR file. The m option is
described in this section.
- Use the u option to update the contents of an
existing JAR file,
including its manifest.
- The Jar tool puts a default manifest with pathname META-INF/MANIFEST.MF
into any JAR file you create.
- You can enable special JAR file
functionality, such as specifying an entry point, by modifying the
default
manifest.
- The Jar tool's m option allows you to add information to
the default manifest during creation of a JAR file.
- You first
prepare a text file containing the information you wish to add to the
default manifest.
- The basic command has this format:
jar cmf manifest-addition jar-file input-file(s)
The options and arguments used in this
command:
- The m option indicates that you want to merge
information from an existing manifest file into the manifest
file of the JAR file you're creating.
- manifest-addition is the name (or path and name) of
the existing text file whose contents you want included in the JAR
file's manifest.
- jar-file is the name that you want the resulting JAR
file to have.
- The input-file(s) argument is a space-separated list
of one or more files that you want to be placed in your JAR file.
The c, m, and f
options can appear in any
order, but there must not be any whitespace between them.
Example
To indicate which class is the project's entry point, add a Main-Class
header to the JAR file's manifest. The header takes the form:
Main-Class: classname
The header's value, classname,
is the name of the class that's the project's entry point.
- Prepare a text file, say mainClass,
consisting of single line with the Main-Class header and
value.
- For example, if your project's main class is Main, your text file has this
line:
- Merge the information in file mainClass into a JAR file's
manifest with a command such as this:
jar cmf mainClass <project_name>.jar class
where class is the directory with your class files.
- You now can run your project from the command line:
java -jar <project_name>.jar
Warning: The
manifest text file must end with a new line or carriage return.
I have success only with jar files whose
Main-Class (e.g., teamname.projectname.Main) is at the top
level of the jar (e.g., not inside a directory, such as class).