×

JAR File in Java

What is a JAR File?

JAR stands for Java Archive. It is a file format mainly used to combine many Java class files and the corresponding metadata and resources into one file. These resources can be anything such as text, images, etc. JAR files help in the distribution of application software or libraries on the JAVA Platform. JAR file format is frequently used nowadays as mostly all the predefined libraries are readily available in this format. It was developed by NetscapeSun Microsystems and Oracle Corporation.

Basically, files that comprise of compressed versions of .class files, image files, directories. Audio files etc. are known as JAR files. These files use the extension .jar typically. Command line operations or any IDE (Integrated Development Environment) can be used to create a .jar file. It is based on the concept of ZIP file format, mainly acting as an archiving tool. WinZip software, who created the ZIP file format can also be used to extract any JAR file.

The main purpose of these files is to use them for tasks like decompression, archiving, lossless data compression and archive unpacking. These tasks decrease the download time of files by reducing their size. Also, JAR files ensure the downloading of Java applets and its requisite components through a single HTTP Transaction. It enhances the speed of loading an applet onto a web page.

JAR supports cross-platform archiving. It is the only file format that is fully extendable and is written in JAVA.  Sound files, image files as well as class files are completely handled by it and ensures backward-compatibility with any existing applet code. Hence, it is recommended to bundle the pieces of java applet.

Java Archive Tool from the Java Development Kit (JDK) can be used to perform various tasks on JAR files.

What is a manifest file?

JAR files consist of metadata files. These metadata files are known as manifest files. They are named as MANIFEST.MF. Manifest files are used to specify the main class of the application, in case any JAR file needs to be used as an executable file. Key-value pairs are used to specify the various properties in these files. Manifest files are often used to know the information about the other files which are packaged.

How to create a manifest file?

The function “Main-Class” along with the class name can be used to create manifest files.

Syntax:

Main-Class: classname

Example:

Main-Class: class1

Here, the name of the class is “class1”. 

NOTE: Default manifest files are created, whenever new .jar files are created. An archive can contain only one manifest file. This manifest file always has a name.

Following is a list of commands that are used to work on .jar file:

How to create a JAR file?

A command known as jar cf  is used to create JAR files. “cf” stands for create file.

Syntax:

jar cf jarfilename inputfiles

Example:

Let’s assume that our package is available in D:\directory ,we write the following command to convert a jar file named as pack.jar: 

D:\> jar cf pack.jar pack

How to view a JAR file?

A command known as jar tf is used to view JAR files. Here, “tf” is used to represent table view of file contents.

Syntax:

jar tf filename

Example:

Let’s assume that our file is available in D:\directory and we want to view the table contents of the file named as newpack.jar

D:\> jar tf newpack.jar

Output:

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

 Here, class1, class2 represents the class files that exist in the package.

How to extract JAR files?

Any standard compression software can be used to extract the contents of a file. A command known as jar xf is used to extract JAR files. Here, “xf” represents extract file.

Syntax:

jar xf filename

Example:

Let us assume that we want to extract the contents of a file named as newpack.jar, present in the D:\directory.

D:\> jar xf newpack.jar

How to update a JAR file?

A command known as jar uf is used to update JAR files. Here, “uf” represents update file. It can be used add files, modify the manifest file or update the contents of a .jar file.

Syntax:

jar uf jarfile inputfile
Example:

Let us assume that we want to update the contents of a file named as newpack.jar, present in the D:\directory.

D:\> jar uf newpack.jar

Related Topics

Construct the Largest Number from the Given Array in Java

In this section, we will create a Java programme that will enable you to locate the greatest integer in an array. The programme will begin comparing the array's numbers with...

3 minutes read.

Java String vs StringBuffer

Java String vs StringBuffer In this section, we will discuss the key differences between String and StringBuffer class. Before moving to the ahead in this section, let’s introduce with both classes. String...

4 minutes read.

Converting Roman to Integer Numerals in java

In this article, you will be acknowledged about the process of conversion of roman numbers to integers in java. The most important approaches are discussed and also the implementation of...

3 minutes read.

How to achieve Multiple Inheritance in Java

Multiple Inheritance is the type of inheritance where one class inherits the properties of more than one super class. For example, class Child inherits (extends) the classes Father and Mother....

4 minutes read.

Arrow Operator in Java

The introduction of arrow functions in ES6 gives you a more precise approach to define JavaScript functions. We can write shorter function syntax thanks to them. Your code will be...

4 minutes read.

Aggregation in Java

Aggregation A "has-a" and "whole/part" connection is the best way to define the aggregate relationship in Java, which exists between two classes. This connection relationship has a higher level of specialisation....

4 minutes read.

How to convert list to String in Java

Sometimes, we need to transform a listing of characters into a string. A string is a chain of characters, so we will make a string from an individual array without...

4 minutes read.

How to convert double to String in Java

How to Convert double to String in Java It is used when we want to convert double primitive to String type. There are two methods to convert double to String. Using String.valueOf()...

2 minutes read.

Counting sort in Java

An array's elements are sorted using the counting sort method, which counts how many times each distinct element appears in the array. The count is kept in an auxiliary array,...

3 minutes read.

Java StringWriter Class

The StringWriter class is a character stream in which it is used to store the output consisting of characters into the string buffer. Upon collecting output into a string buffer,...

3 minutes read.

Operators in Java

There is a good operator environment provided by Java. These operators are divided into four different groups i.e. arithmetic, bitwise, relational, and logical. There are other operators also available to...

7 minutes read.

String Palindrome Program in Java

String Palindrome Program in Java The palindrome is a string, phrase, word, number, or other sequences of characters that can be read in both directions i.e. forward (left to right) and...

11 minutes read.

Best Java Libraries

One of the most widely used programming languages is Java. Java has a large number of libraries, including the standard Java library that includes libraries such as java.lang, java.util, and...

7 minutes read.

Sudoku in Java

Sudoku is a combinatorial-number-placement puzzle with a logic-based approach. The goal of a traditional Sudoku puzzle is to fill in the numbers on a 9 by 9 grid so that...

6 minutes read.

Java LinkedHashMap

LinkedHashMap implements the Map interface. It inherits the HashMap class. Some essential features of LinkedHashMap are: It contains the values based on the keys. It maintains the order of insertion. It contains unique...

5 minutes read.

Java Integer toUnsignedString() method

The toUnsignedString() method of Java Integer class returns a string representation of the argument as an unsigned decimal value. The second syntax returns a string representation of the given argument as...

2 minutes read.

Get yesterdays date by no of days in Java

In this tutorial, we are going to learn how to get yesterday’s date by the no of days in Java. Using the Calendar class, one can get the current date....

1 minute read.

Dictionary in Java

Dictionary in Java The Dictionary class represents a key-value relation which maps keys to values. In Dictionary class, every key and every value is an object. It is an abstract class associated with...

2 minutes read.

public static void main string args meaning in java

In java main() method is the initial point for execution of the program. If a program doesn’t contain the main method, the program will not execute. JVM(java virtual machine) starts...

3 minutes read.

MVC in Java

A well-known design pattern is Model-View-Controller. The discipline of web development. We can organize our code in this manner. The document stipulates that a program or application must include a...

4 minutes read.