×

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 Netscape, Sun 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

How to run java program in ubuntu

To run the java programming in ubuntu, we need to follow several steps: Let's see the process. Step1: Install the Java compiler or JDK to the system. To run the java program, we...

3 minutes read.

Why to use Enum in Java?

In this article, you will be acknowledged about the Enum in java, its uses and mainly its purpose. Enum has various functionalities. Each of them will be discussed. Enum In a computer...

3 minutes read.

Java Boolean toValue() Method

The valueOf() method of Java Boolean class returns a Boolean object representing the given Boolean or String value. It returns true, if the specified Boolean or string object is true...

2 minutes read.

How to run Java Program in Command Prompt

How to run Java Program in Command Prompt In this section, we will learn how to write, save, compile, and execute or run a Java program in the Command Prompt. Note: One...

3 minutes read.

Array Programs in Java

Array Programs in Java: An array is a data structure that stores similar elements in a contiguous memory location. In Java, an array is an object that stores the same...

7 minutes read.

Packages in Java

Packages in Java can be defined as an assortment for grouping various classes and interfaces based on their performance. It is a catalog for holding various java files. They provide...

4 minutes read.

How to Calculate Week Number From Current Date in Java?

The WeekFields class's weekOfMonth() method is utilized to return the field for access the week of a month based on this WeekFields. If the first day of the month is a...

3 minutes read.

Java Error Stack Trace

The stack trace in Java is an array of stacks.The stack trace reveals the console's location of an exception or error by gathering data from all program methods. The JVM...

3 minutes read.

How to Convert Object to String in Java

How to Convert Object to String in Java You can convert any Object to String in Java whether it is a user-defined class, StringBuilder or StringBuffer, etc. There are two methods...

2 minutes read.

Shallow copy in Java

Java's most important task is making a copy or clone of an object. In this part, we'll talk about shallow copies in Java and how to make them of Java...

4 minutes read.

Java time local date

Java: Java is one of the programming language which is object oriented. It consists of many features such as robust, simple, architecture neutral, dynamic, distributed, multi threaded, portable etc. The main feature...

3 minutes read.

Java Math exp() Method

The exp() method of Math class returns Euler’s number(e) raised to the power of a double value. Syntax: public static double exp(double a) Parameters: The parameter ‘a’ represents the exponent e. Return Value: The exp ()...

2 minutes read.

Bucket Sort in Java

Bucket Sort in JavaBucket sort is also called bin sort. Bucket sort first puts the elements of the array or list into different buckets. The first bucket contains elements of the smallest value. The...

11 minutes read.

Java Math with Methods and Examples

Java Math class contains various methods for performing math operations like min(), max(), avg() and various trigonometric functions like sin(), cos(), tan() etc. Methods: The java.lang.Math class contains various methods for performing...

5 minutes read.

Why String in Immutable in Java?

Why String in Immutable in Java Immutable means unchangeable or unmodifiable.  Strings in Java are immutable, it means once a string is created, it cannot be modified or changed. Any change...

2 minutes read.

Package Program in Java

Package Program in Java The package program in Java helps us to understand the significance of packages in Java. Packages are mainly used to group together similar classes, interfaces and sub-packages....

6 minutes read.

Bad Operand types for Binary Operator Java

We will discuss how to handle issues in bad operand types for binary operator &, bad operand types for binary operator &&, bad operand types for binary operator ==, and...

4 minutes read.

Java Math rint() Method

The rint() method of Java Math class returns the double value which is close to the specified argument and is equal to mathematical integer. Syntax: public static double rint(double a) Parameters: The parameter ‘a’...

1 minute read.

Java Maven Silicon

Maven is a build automation tool that is mostly used for Java applications. It allows you to manage the build, reporting, and documentation of a project from the central location. Maven provides...

4 minutes read.

How to enable java in chrome

The Java module is huge for the Java Runtime Environment (JRE). It permits a program to work with the Java stage to run Java applets. Essentially, each of the undertakings connect...

3 minutes read.