0% found this document useful (0 votes)
6 views3 pages

JAR Files in Java

A JAR (Java Archive) file is a package format used to aggregate Java class files and associated resources for distribution on the Java platform. The document explains how to create, view, extract, update, and run JAR files using specific commands. It also highlights the importance of the manifest file included in JAR files.

Uploaded by

P Sakthikumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

JAR Files in Java

A JAR (Java Archive) file is a package format used to aggregate Java class files and associated resources for distribution on the Java platform. The document explains how to create, view, extract, update, and run JAR files using specific commands. It also highlights the importance of the manifest file included in JAR files.

Uploaded by

P Sakthikumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

17/05/2025, 20:03 JAR files in Java | GeeksforGeeks

Search... 99+

Java Course Java Arrays Java Strings Java OOPs Java Collection Java 8 Tutorial Java Multith

JAR files in Java


Last Updated : 26 Jul, 2023

A JAR (Java Archive) is a package file format typically used to aggregate


many Java class files and associated metadata and resources (text,
images, etc.) into one file to distribute application software or libraries
on the Java platform.
In simple words, a JAR file is a file that contains a compressed version of
.class files, audio files, image files, or directories. We can imagine a .jar
file as a zipped file(.zip) that is created by using WinZip software. Even,
WinZip software can be used to extract the contents of a .jar . So you
can use them for tasks such as lossless data compression, archiving,
decompression, and archive unpacking.

Let us see how to create a .jar file and related commands which help us
to work with .jar files

1.1 Create a JAR file


In order to create a .jar file, we can use jar cf command in the following
ways as discussed below:

Syntax:

jar cf jarfilename inputfiles

Here, cf represents to create the file. For example , assuming our


package pack is available in C:\directory , to convert it into a jar file
into the pack.jar , we can give the command as:

C:\> jar cf pack.jar pack


We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Got It !
https://www.geeksforgeeks.org/jar-files-java/ 1/8
17/05/2025, 20:03 JAR files in Java | GeeksforGeeks

1. 2 View a JAR file


Now, the pack.jar file is created. In order to view a JAR file '.jar' files, we
can use the command as:

Syntax:

jar tf jarfilename

Here, tf represents the table view of file contents. For example, to view
the contents of our pack.jar file, we can give the command:

C:/> jar tf pack.jar

Now, the contents of pack.jar are displayed as follows:

META-INF/
META-INF/MANIFEST.MF
pack/
pack/class1.class
pack/class2.class
..
..

Here class1, class2, etc are the classes in the package pack. The first
two entries represent that there is a manifest file created and added to
pack.jar. The third entry represents the sub-directory with the name
pack and the last two represent the files name in the directory pack.

Note: When we create .jar files, it automatically receives the


default manifest file. There can be only one manifest file in an
archive, and it always has the pathname.

META-INF/MANIFEST.MF

We use cookies
This to ensure
manifest fileyou
is have the best
useful to browsing
specifyexperience on our website.
the information By using
about our site,
other you
files
acknowledge that you have read and understood our Cookie Policy & Privacy Policy
which are packaged.

https://www.geeksforgeeks.org/jar-files-java/ 2/8
17/05/2025, 20:03 JAR files in Java | GeeksforGeeks

1.3 Extracting a JAR file


In order to extract the files from a .jar file, we can use the commands
below listed:

jar xf jarfilename

Here, xf represents extract files from the jar files. For example, to
extract the contents of our pack.jar file, we can write:

C:\> jar xf pack.jar

This will create the following directories in C:\

META-INF

In this directory, we can see class1.class and class2.class.

pack

1.4 Updating a JAR File


The Jar tool provides a 'u' option that you can use to update the
contents of an existing JAR file by modifying its manifest or by adding
files. The basic command for adding files has this format as shown
below:

Syntax:

jar uf jar-file input-file(s)

Here 'uf' represents the updated jar file. For example, to update the
contents of our pack.jar file, we can write:

C:\>jar uf pack.jar

1.5 Running a JAR file


In order to run an application packaged as a JAR file (requires the Main-
class manifest header), the following command can be used as listed:
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
Syntax: acknowledge that you have read and understood our Cookie Policy & Privacy Policy

https://www.geeksforgeeks.org/jar-files-java/ 3/8

You might also like