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 add JAR file
functionality, such as specifying an entry point, by modifying the
default
manifest.
- The m option allows you to add information to
the default manifest while creating a JAR file.
- First,
create a text file, manifest-addition, with the information you wish to add (see Example below).
- 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/name) of
the existing text file whose contents you want included in the JAR
file's manifest.
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 manifest-addition
consisting of the 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 manifest-addition into a JAR file's
manifest:
jar cmf manifest-addition <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.
Exercise
- Make a file indicating that teamname.projectname.Main is the entry point for your project.
- Modify the jar command that creates the projectname.jar so that it includes this manifest information.
- Give a command to run your project, given the projectname.jar file.