A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/working-with-jar-and-manifest-files-in-java/ below:

Working with JAR and Manifest Files In Java

Working with JAR and Manifest Files In Java

Last Updated : 23 Jul, 2025

A Jar file stands for Java archives, it is a compressed archive that contains the Java classes, libraries, metadata, and other files such as media data, audio, video, and documents. When we want to distribute a version of the software or distribute a single file, and not a directory structure filled with class files. All the files in a JAR file are compressed using a format similar to ZIP. The manifest file is used to store the details of JAR files the metadata of JAR files, for example, such as the details of the main file where the execution starts.

What is a JAR File?

A JAR file is a compressed archive that combines multiple files into one package. The JAR file can be created using the jar tool. It is a command-line tool that executes on the basis of commands, and we can find the command by using jar --help.

Syntax:

jar options jar-file [manifest-file] file1 file2 file3 ...

Important jar Command Options Creating a JAR file

For creating a JAR file which have two classes server.class and client.class and a Jpeg image logo.jpeg, one need to write following command :

jar cvf chat.jar server.class client.class logo.jpeg

Note: It’s a better practice to use -v option along with jar command and it will get to know how the things are going on.

What is a Manifest File?

JAR file contains a manifest file that describe the features of the archive. Each JAR file have a manifest file by default. Default manifest file is named as MANIFEST.MF and is present in the META-INF subdirectory of archive. Although the default manifest file contains just two entries, but complex manifest files can have way more.

MANIFEST.MF file:

Manifest files are grouped into sections. Each section have two entries section name and its value. We will see a bit later how these sections can really help us in controlling the properties of our archive. Manifest file can also be updated by using the m option of jar command. But there are certain things which need to kept in mind while updating manifest file otherwise it may throw the exception.

java.io.IOException: invalid manifest format

Updating Manifest File:

To add metadata (like the main class), create a file manifest.txt:

Main-Class: client

Then update your JAR using:

jar uvfm chat.jar manifest.txt

Here manifest.txt is the new manifest file, which has following contents:

The output of above command will be somewhat like this:

Here we are getting two warnings because we are trying to overwrite to previously present entries.

Important points while handling the Manifest files:

Steps to Create a JAR file and Manifest file

Step 1: Open the source directory in which we want to compress the files into a single JAR file.

Step 2: Now we use the jar commands to create a jar file

jar cvfe chat.jar client client.class server.class logo.jpeg

Note: Remember not to add .class extension after the name of class we can set the main class.

Step 3: We can add a Main-Class entry in the manifest file and then update it.

Main-Class: client

Step 4: Running the JAR file

java -jar chat.jar

Depending on operating system configuration, users may even be able to launch application by double clicking the JAR file icon.

Package Sealing

Package sealing in Java is a process which used to ensure that no further classes can add themselves to a jar file..Without package sealing, other classes can add themselves to the same package and thereby gain access to package visible features. By default the packages in a jar file are not sealed but one can change the global default by adding few lines in manifest file.

Steps to Seal a Package

Step 1: Go to the directory in which file we want to seal for example we are sealing the chat.jar archive.

Step 2: Update the MANIFEST.MF file add the following lines in the seal package.

Name: application
Sealed: true

Step 3: After making the changes update the jar file.

Important Points:



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4