Thursday, September 29, 2022
HomeSoftware DevelopmentHow one can Work With a JAR Manifest File in Java

How one can 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 incorporates metadata associated to recordsdata contained in a programmer’s JAR archive. This metadata can vary from data regarding 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 grasp the best way to work with these manifest recordsdata and the best way to use them correctly. On this Java tutorial, you’ll learn to work with a JAR manifest file in your Java purposes.

Learn: The Finest Instruments for Distant Builders

Default Manifest Information in Java

If you create a JAR file, a default manifest file can be created with it. This file initially incorporates two fields regarding 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 all the time situated within the path META-INF/MANIFEST.MF of your JAR file.

Learn: Tricks to Enhance Efficiency in Java

How one can Modify a Manifest File in Java

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

As you’ll have observed from the earlier part, JAR recordsdata comprise header:worth pairs. You too can add an entry on your desired header:worth pair. Nevertheless, you will need to notice 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 also can use the m possibility to switch JAR manifest recordsdata, as proven under:

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

The m possibility specifies that you just intend to merge sure data from a textual content file (reminiscent of add-to-manifest.txt) to your present JAR manifest file.

It will be significant for builders to notice that the order during 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 following two sections describe some explicit use circumstances that you may apply along with your JAR file.

JAR Information and Java Utility Packaging

JAR recordsdata are typically used to package deal Java purposes. If you determine to run an utility in a JAR file, programmers must specify the entry level to make use of. An entry level is the category during which your principal() methodology is outlined. To outline your utility’s entry level, use the Important-Class header.

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

Important-Class: PackageName.ClassName

To run the applying in your JAR archive, now you can use the command under:

java -jar  ArchiveName.jar

You too can set the entry level on your utility utilizing the e possibility of your JAR instrument. You will need to notice that this feature overrides the Important-Class header in your manifest file.

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

jar cfe ArchiveName.jar * MyClass.class

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

jar cfe ArchiveName.jar * MyDirectory/MyClass.class

Learn: Finest DevOps and DevSecOps Instruments

How one can Add Courses to JAR Classpath

When packaging your purposes, programmers could place completely different lessons in a number of jar recordsdata. A superb instance right here can 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 while you run it, that you must 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 Information

A manifest file permits builders to explain data regarding recordsdata of their JAR archive. As beforehand talked about, all the time 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 improvement tutorials.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments