JAR File in Java (Create, View, Extract, Update, Run) – All in One Place

JAR file stands for Java Archive. JAR file in java is a compressed version of the normal one. The JAR file is used to store many types of files like audio file, video file, etc. This format is mainly used to distribute a set of Java classes. Using this, the downloading time decreased because of the small size of the file. It can be executed from the java i.e., Java Web Start. To create the executable JAR file, you need to create a manifest file which is .mf file.

Before we start, you should brush up the knowledge of Classes & Objects in Java.

For example, if we have data related to products like seller name, product version, date of making of the item and different things related to the product are mentioned in the manifest file. Such type of files is used in the JAR file for making it executable. It used for collecting additional files associated with the components.

Introduction to JAR Files in Java

The JAR file format is based on zip format. These types of files are mainly used for implementing libraries. Java Virtual Machine understands and implements these types of format for the java application. To perform basic operations related to the JAR file, we use the Java Archive Tool (JAR tool). This tool is provided by the Java Development Kit (JDK)

1. Creating & Viewing a JAR File in Java

We use jar cf command to create this file.

Syntax –

jar cf jarfilename inputfiles
//cf represents the creation of file.
C:\> jar cf pack.jar pack

Viewing a JAR File in Java

Command –

jar tf jarfilename
// tf represents the contents of file

Recommended Reading – 4 Types of Java Inner Class

Example –

Join DataFlair on Telegram
C:/> jar tf pack.jar
// viewing contents of pack.jar

Output –
Jar-File-In-Java

2. Extracting & Updating

Command for Extracting –

jar xf jarfilename
//xf represents extracting of files
C:\> jar xf pack.jar

Command for Updating –

jar uf jar-file input-file(s)
// updating the file

Example –

C:\>jar uf pack.jar

3. Running

Command –

C:\>java -jar pack.jar
// requires a main class manifest header

Summary

With the help of this article, you can create, view, extract, update, and run JAR files in Java. This file is used to aggregate many class files, audio files, image files, or directories.

Hope, you like the explanation. Share your experience with us in the comment section.

Do you know what is Singleton Class in Java and how to implement it?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.