JavaHow to Work With a JAR Manifest File in Java

How to Work With a JAR Manifest File in Java content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Java Developer Tutorials
In the Java programming language, a manifest file is a text file that contains metadata related to files contained in a programmer’s JAR archive. This metadata can range from information relating to package information to attributes of security properties.

A program can only have one manifest file in its JAR archive, so it is important for developers to understand how to work with these manifest files and how to use them properly. In this Java tutorial, you will learn how to work with a JAR manifest file in your Java applications.

Read: The Best Tools for Remote Developers

Default Manifest Files in Java

When you create a JAR file, a default manifest file is also created with it. This file initially contains two fields relating to the manifest version and the JDK version. For example, here is a typical manifest file’s contents:

Manifest-Version: 1.0
Created-By: 11.0.3 (Oracle Corporation)

The default manifest is always located in the path META-INF/MANIFEST.MF of your JAR file.

Read: Tips to Improve Performance in Java

How to Modify a Manifest File in Java

Developers can either modify a manifest file manually or by using the m option at the creation of a JAR file. Manually modifying the file means directly adding entries to it.

As you may have noticed from the previous section, JAR files contain header:value pairs. You can also add an entry for your desired header:value pair. However, it is important to note that, after the last entry, programmers should add a new line character. Otherwise, the last entry will not be parsed properly.

Alternatively, coders can also use the m option to modify JAR manifest files, as shown below:

jar cfm ArchiveName.jar  add-to-manifest.txt files-to-include

The m option specifies that you intend to merge certain information from a text file (such as add-to-manifest.txt) to your existing JAR manifest file.

It is important for developers to note that the order in which the f and m options appear should match that of their respective arguments. You also need to ensure that your text file ends with a new line character.

The next two sections describe some particular use cases that you can apply with your JAR file.

JAR Files and Java Application Packaging

JAR files are sometimes used to package Java applications. When you decide to run an application in a JAR file, programmers need to specify the entry point to use. An entry point is the class in which your main() method is defined. To define your application’s entry point, use the Main-Class header.

For example, if your entry point is ClassName in the package PackageName, then you should include the following entry in your manifest file:

Main-Class: PackageName.ClassName

To run the application in your JAR archive, you can now use the command below:

java -jar  ArchiveName.jar

You can also set the entry point for your application using the e option of your JAR tool. It is important to note that this option overrides the Main-Class header in your manifest file.

The Java code example below creates an archive named ArchiveName.jar with the entry point set to MyClass.class:

jar cfe ArchiveName.jar * MyClass.class

You can also use the e option when updating your JAR archive. If your entry point is in a directory (say MyDirectory), then you can use the forward-slash ‘/’ when specifying it in the command line:

jar cfe ArchiveName.jar * MyDirectory/MyClass.class

Read: Best DevOps and DevSecOps Tools

How to Add Classes to JAR Classpath

When packaging your applications, programmers may place different classes in several jar files. A good example here would be if an applet packaged in a JAR archive has dependencies packaged in different JAR archives.

To ensure that classes in another JAR archive are loaded into your JAR file when you run it, you need to add their classpath in your calling JAR file.

Simply do this by setting the path in the Class-Path header in your manifest file. For example, if the JAR file to be loaded is in the same directory as your calling JAR file, you could use the following:

Class-Path: Other-JAR-File.jar

Final Thoughts on Java Manifest Files

A manifest file allows developers to describe information relating to files in their JAR archive. As previously mentioned, always remember to end the modifying text file of your JAR file with a new line character.

Read more Java programming and software development tutorials.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories