Working with application JAR files

A JAR (Java Application aRchive) is a common way to package and distribute Java applications and libraries. It includes Java class files, resources, and other files.

It is a good practice to include a manifest.mf file in the JAR file contains information about the file, such as the name of the application or library, the version number, and the list of files included in the JAR file.

Working

Working with JAR files includes various tasks, such as

  • Creating JAR file.

  • Viewing JAR file content.

  • Extracting files from JAR.

  • Executing JAR file.

  • Updating JAR file content.

We can implement these tasks using jar commands in cmd, but to use jar, we need to install JDK (Java Development Kit).

We can download JDK from here.

Let's explore these tasks in detail.

Creating JAR file

We can create a JAR file by running the following command.

jar cf jar-file input-file(s)

Arguments

The arguments used in the command are described below.

  • c indicates that we want to create JAR file.

  • f indicates that we want to create a file, not just to show output in stdout.

  • jar-file can be any name that we want to give to the JAR file.

  • input-file(s) can be a single or multiple files separated by space that we want to include in JAR file.

Options

We also have some arguments we can include in our command instead of cf or in addition to them without any space. These options are:

  • v creates an output on stdout showing the name of each file while the JAR file is being built.

  • 0 (zero) indicates that we don't want the JAR file to be compressed.

  • M indicates that default manifest file should not be produced.

  • m indicates that we want to include information from the previous existing manifest file in the new manifest file. Its format is:

jar cmf jar-file existing-manifest input-file(s)

Viewing JAR file content

We can view the JAR file content by running the following command.

jar tf jar-file

Arguments

  • t indicates that we want to view the JAR file's content table.

  • f indicates the JAR file whose content is to be viewed.

  • jar-file indicates the path or the name of the JAR file to be viewed. If we replace it with a JAR file name such as "myApplicatiom.jar" then it will show the content of the file to stdout as:

META-INF/MANIFEST.MF
myApplicatiom.class
audio/
audio/beeping.au
images/
images/crossing.gif

Updating JAR file content

We can update the JAR file content by running the following command.

jar uf jar-file input-file(s)

After making a few changes, such as changing jar-file to "myApplicatiom.jar" and input-file(s) to "images/newfile.gif" and then running the above command, a file name "newfile.gif" will be added to the JAR file in the images folder, and the table of content is shown on the command line as:

META-INF/MANIFEST.MF
myApplicatiom.class
audio/
audio/beeping.au
images/
images/crossing.gif
images/newfile.gif

Arguments

  • u indicates that we want to update the JAR file.

  • f indicates the JAR file whose content is to be viewed.

  • jar-file is the existing JAR file that we want to update.

  • input-file(s) is the file name to be added; we can also write multiple files separated by space.

Extracting files from JAR

We can extract the JAR file content by running the following command.

jar xf jar-file [archived-file(s)] 

If we run the above command after making a few changes, such as jar xf myApplication.jar, it will extract all the files of the JAR file into the same directory.

META-INF/MANIFEST.MF
myApplicatiom.class
audio/
audio/beeping.au
images/
images/crossing.gif
images/newfile.gif

Arguments

  • x option indicates that we want to extract files from the JAR file.

  • f indicates the JAR file whose content is to be viewed.

  • jar-file is the existing JAR file that we want to update.

  • archived-file(s) is an optional argument consisting of the number of files separated by spaces we want to extract from the JAR file.

Executing JAR file

We can execute the JAR file by running the following command.

java -jar jar-file

Arguments

  • -jar indicates that the application is in JAR format.

  • jar-file is the existing JAR file that we want to execute.

We must also specify which class is the entry point to run the application, so we must add a Main-Class header to the JAR file's manifest.mf file.

Review

1

Which file in a JAR contains information about the application or library?

A)

manifest.mf

B)

appinfo.xml

C)

config.ini

D)

info.properties

Question 1 of 30 attempted

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved