JAR files support a wide range of functionality, including electronic signing, version control, package sealing, and others. What gives a JAR file this versatility? The answer is the JAR file’s manifest.
The manifest is a special file that can contain information about the files packaged in a JAR file. By tailoring this “meta” information that the manifest contains, you enable the JAR file to serve a variety of purposes.
Default Manifest
- When you create a JAR file, it automatically receives a default manifest file. There can be only one manifest file in an archive, and it always has the below pathname
META-INF/MANIFEST.MF
- When you create a JAR file, the default manifest file simply contains the following:
Manifest-Version: 1.0 Created-By: 1.7.0_06 (Oracle Corporation)
- The manifest can also contain information about the other files that are packaged in the archive.
- Exactly what file information should be recorded in the manifest depends on how you intend to use the JAR file.
- The default manifest makes no assumptions about what information it should record about other files.
Modifying a Manifest File
- The Jar tool automatically puts a default manifest with the pathname META-INF/MANIFEST.MF into any JAR file you create.
- You can enable special JAR file functionality, such as package sealing, by modifying the default manifest.
- Typically, modifying the default manifest involves adding special-purpose headers to the manifest that allow the JAR file to perform a particular desired function.
The basic command has this format:
jar cfm jar-file manifest-addition input-file(s)
- The c option indicates that you want to create a JAR file.
- The m option indicates that you want to merge information from an existing file into the manifest file of the JAR file you’re creating.
- The f option indicates that you want the output to go to a file (the JAR file you’re creating) rather than to standard output.
- manifest-addition is the name (or path and name) of the existing text file whose contents you want to add to the contents of 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.
Setting an Application’s Entry Point
- If you have an application bundled in a JAR file, you need some way to indicate which class within the JAR file is your application’s entry point. You provide this information with the
Main-Class
header in the manifest, which has the general form:
Main-Class: classname
- The value
classname
is the name of the class that is your application’s entry point. - Recall that the entry point is a class having a method with signature
public static void main(String[] args)
. - After you have set the
Main-Class
header in the manifest, you then run the JAR file using the following form of thejava
command:
java -jar JAR-name
- The
main
method of the class specified in theMain-Class
header is executed.
Example :
We want to execute the main
method in the class MyClass
in the package MyPackage
when we run the JAR file.
We first create a text file named Manifest.txt
with the following contents:
Main-Class: MyPackage.MyClass
Read Me :
https://docs.oracle.com/javase/tutorial/deployment/jar/modman.html
- Warning: The text file must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.
We then create a JAR file named MyJar.jar
by entering the following command:
jar cfm MyJar.jar Manifest.txt MyPackage/*.class
This creates the JAR file with a manifest with the following contents:
Manifest-Version: 1.0 Created-By: 1.7.0_06 (Oracle Corporation) Main-Class: MyPackage.MyClass
When you run the JAR file with the following command, the main
method of MyClass
executes:
java -jar MyJar.jar
Setting an Entry Point with the JAR Tool
The ‘e’ flag (for ‘entrypoint’) creates or overrides the manifest’s Main-Class
attribute. It can be used while creating or updating a JAR file. Use it to specify the application entry point without editing or creating the manifest file.
For example, this command creates app.jar
where the Main-Class
attribute value in the manifest is set to MyApp
:
jar cfe app.jar MyApp MyApp.class
You can directly invoke this application by running the following command:
java -jar app.jar
If the entrypoint class name is in a package it may use a ‘.’ (dot) character as the delimiter. For example, if Main.class
is in a package called foo
the entry point can be specified in the following ways:
jar cfe Main.jar foo.Main foo/Main.class
Adding Classes to the JAR File’s Classpath
You may need to reference classes in other JAR files from within a JAR file.
For example, in a typical situation an applet is bundled in a JAR file whose manifest references a different JAR file (or several different JAR files) that serves as utilities for the purposes of that applet.
You specify classes to include in the Class-Path header field in the manifest file of an applet or application. The Class-Path header takes the following form:
Class-Path: jar1-name jar2-name directory-name/jar3-name
By using the Class-Path header in the manifest, you can avoid having to specify a long -classpath flag when invoking Java to run the your application.
Note: The Class-Path header points to classes or JAR files on the local network, not JAR files within the JAR file or classes accessible over Internet protocols. To load classes in JAR files within a JAR file into the class path, you must write custom code to load those classes. For example, if MyJar.jar contains another JAR file called MyUtils.jar, you cannot use the Class-Path header in MyJar.jar’s manifest to load classes in MyUtils.jar into the class path.
An Example
We want to load classes in MyUtils.jar into the class path for use in MyJar.jar. These two JAR files are in the same directory.
We first create a text file named Manifest.txt with the following contents:
Class-Path: MyUtils.jar
Warning: The text file must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.
We then create a JAR file named MyJar.jar by entering the following command:
jar cfm MyJar.jar Manifest.txt MyPackage/*.class
This creates the JAR file with a manifest with the following contents:
Manifest-Version: 1.0 Class-Path: MyUtils.jar Created-By: 1.7.0_06 (Oracle Corporation)
The classes in MyUtils.jar are now loaded into the class path when you run MyJar.jar.
ReadMe : https://docs.oracle.com/javase/tutorial/deployment/jar/packageman.html