Open In App

Working with JAR and Manifest Files In Java

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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 ...

Lightbox

Important jar Command Options

  • jar – file : name of the jar file on which you want to use the jar tool.
  • file1, file2, file3 : files which you want to add inside a jar file. manifest-file is the name of file which contains manifest of that jar file, giving manifest-file as an argument is entirely optional.
  • c : Creates a new or empty archive and adds files to it. If any of the specified file name are directories, then the jar program processes them recursively.
  • C : Temporarily changes the directory.
  • e : Creates an entry point in the manifest.
  • f : Specifies the JAR file name as the second command-line argument. If this parameter is missing, jar will write the result to standard output (when creating a JAR file)or read it from standard input(when extracting or tabulating a JAR file).
  • i : Creates an index file.
  • m : Adds a manifest file to the JAR file. A manifest is a description of the archive contents and origin. Every archive has a default manifest, but you can supply your own if you want to authenticate the contents of the archive.
  • M : Does not create a manifest file for the entries.
  • t : Displays the table of contents.
  • u : Updates an existing JAR file.
  • v : Generates verbose output.
  • x : Extract files. If you supply one or more file names, only those files are extracted Otherwise, all files are extracted.
  • 0 : Stores without zip compression.

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:

Default manifest

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:


manifest.txt


The output of above command will be somewhat like this:


null


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

Important points while handling the Manifest files:

  • We need to put a space between the name and value of any section in manifest file. like Version:1.1 is in valid section instead write Version: 1.1 that space between colon.
  • While specifying the main class not add .class extension at the end of class name. Simply specify the main class by typing. Main-Class: Classname
  • must add newline at the end of file. You need not to write \n for specifying newline instead just leave the last line of your manifest file blank that will serve the purpose.
  • Must add newline at the end of file. We can not add \n for specifying newline instead just leave the last line of manifest file blank that will serve the purpose.
  • Text file for manifest must use UTF-8 encoding.

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

jarCommand1

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

  • Name: Specify the package name we want to seal.
  • Sealed: Write this property true then the file will be sealed.

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

Important Points:

  • JAR file helps to bundle multiple files and resources into a single unit which helps to transfer the code base more efficiently and easier manner.
  • The MANIFEST.MF located in the META-INF directory which containing the information about the files.
  • If we seal a package so it will prevent the package to additionally add the new classes.
  • While creating the Manifest file make sure that to follow the formatting rules like do not add the .class extension and ensure the space between sections.

Article Tags :
Practice Tags :

Similar Reads