Work With a JAR Manifest File in Java


Java Developer Tutorials

Within the Java programming language, a manifest file is a textual content file that accommodates metadata associated to recordsdata contained in a programmer’s JAR archive. This metadata can vary from data referring to package deal data to attributes of safety properties.

A program can solely have one manifest file in its JAR archive, so it is vital for builders to know how one can work with these manifest recordsdata and how one can use them correctly. On this Java tutorial, you’ll discover ways to work with a JAR manifest file in your Java purposes.

Learn: The Finest Instruments for Distant Builders

Default Manifest Recordsdata in Java

While you create a JAR file, a default manifest file can be created with it. This file initially accommodates two fields referring to the manifest model and the JDK model. For instance, here’s a typical manifest file’s contents:

Manifest-Model: 1.0
Created-By: 11.0.3 (Oracle Company)

The default manifest is at all times positioned within the path META-INF/MANIFEST.MF of your JAR file.

Learn: Tricks to Enhance Efficiency in Java

Modify a Manifest File in Java

Builders can both modify a manifest file manually or by utilizing the m possibility on the creation of a JAR file. Manually modifying the file means straight including entries to it.

As you could have seen from the earlier part, JAR recordsdata comprise header:worth pairs. You may as well add an entry on your desired header:worth pair. Nevertheless, you will need to be aware that, after the final entry, programmers ought to add a brand new line character. In any other case, the final entry won’t be parsed correctly.

Alternatively, coders may also use the m possibility to switch JAR manifest recordsdata, as proven beneath:

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

The m possibility specifies that you simply intend to merge sure data from a textual content file (equivalent to add-to-manifest.txt) to your current JAR manifest file.

It will be significant for builders to notice that the order by which the f and m choices seem ought to match that of their respective arguments. You additionally want to make sure that your textual content file ends with a brand new line character.

The subsequent two sections describe some explicit use instances that you would be able to apply along with your JAR file.

JAR Recordsdata and Java Utility Packaging

JAR recordsdata are generally used to package deal Java purposes. While you resolve to run an software in a JAR file, programmers must specify the entry level to make use of. An entry level is the category by which your principal() technique is outlined. To outline your software’s entry level, use the Most important-Class header.

For instance, in case your entry level is ClassName within the package deal PackageName, then you must embrace the next entry in your manifest file:

Most important-Class: PackageName.ClassName

To run the appliance in your JAR archive, now you can use the command beneath:

java -jar  ArchiveName.jar

You may as well set the entry level on your software utilizing the e possibility of your JAR software. You will need to be aware that this feature overrides the Most important-Class header in your manifest file.

The Java code instance beneath creates an archive named ArchiveName.jar with the entry level set to MyClass.class:

jar cfe ArchiveName.jar * MyClass.class

You may as well use the e possibility when updating your JAR archive. In case your entry level is in a listing (say MyDirectory), then you should use the forward-slash ‘/’ when specifying it within the command line:

jar cfe ArchiveName.jar * MyDirectory/MyClass.class

Learn: Finest DevOps and DevSecOps Instruments

Add Courses to JAR Classpath

When packaging your purposes, programmers could place totally different lessons in a number of jar recordsdata. instance right here could be if an applet packaged in a JAR archive has dependencies packaged in several JAR archives.

To make sure that lessons in one other JAR archive are loaded into your JAR file once you run it, you could add their classpath in your calling JAR file.

Merely do that by setting the trail within the Class-Path header in your manifest file. For instance, if the JAR file to be loaded is in the identical listing as your calling JAR file, you would use the next:

Class-Path: Different-JAR-File.jar

Closing Ideas on Java Manifest Recordsdata

A manifest file permits builders to explain data referring to recordsdata of their JAR archive. As beforehand talked about, at all times keep in mind to finish the modifying textual content file of your JAR file with a brand new line character.

Learn extra Java programming and software program growth tutorials.


Please enter your comment!
Please enter your name here