Introduction
This article explains the JAR format used in Java. It explains what a JAR is, its advantages, how to create them, how to view the components and how to extract files from JAR files.
What is JAR
A JAR (Java ARchive) file is a compressed file that contains class, sound, image and textual files for a Java application in a single file. The JAR format is based on the popular zip file format and is somewhat comparable to the Unix tar file. When we are using the Java Development Kit (JDK) a JAR file included in it is provided by Sun Microsystems. A Java application can be started with a set of JAR files for use during execution. An off-the-shelf open source package can be delivered as a JAR file and run with XML data.
Java classes are packaged and distributed in JAR format. It is a compression/decompression format provided by Sun Microsystems for compression and decompression of Java classes.
Sun Microsystems provides all the classes of the core Java library through a JAR file name "rt.jar" (it is contained in the "C:\Program Files\Java\jdk1.7.0_25\jre\lib" folder).
Advantages of using JAR files
The JAR File format provides the following benefits.
By providing a digital signature in our JAR File, we enhance the security of Java files; as the authorized user who recognize our signature can access the files.
It is easy to handle, create, maintain, update, etcetera.
It is basically used to compress the size of the files. Suppose we are developing a software that contains many files so we need a tool to compress the size of this files so the JAR tool is used to compress the size.
If we are using a JAR file then we need to download only one single file hence it reduces the download time since we don't need to download multiple files.
Using this tool, we bundled our various types of files (like text, audio, video, images, etcetera) in a single place. So in terms of development it reduces the burden of storing multiple files in a single folder. Packaging is a very good factor for software developers since we have a single JAR file that is authenticated using asignature. So JARs makes it efficient in storage as well as increases its security.
For creating and managing JAR files Sun, Microsystems provides tools called JAR as part of the JDK.
To view the commands of the JAR tools open a command prompt and type:
Now press Enter and see the commands of the JAR tools as in the following:
Now to perform several operation using JAR Tools.
For a sample test, add some files in a folder. Let's create a new folder; I created a new folder in the "E:\Sandy" location named "jar" and added a file in it. The following Java files are added.
We create two Java files named First.java and Second.java.
First.java
class First
{
public static void main(String args[])
{
System.out.println("It is a first java file");
}
}
Second.java
class Second
{
public static void main(String args[])
{
System.out.println("It is the second java file");
}
}
Now compile both files separately. Then we see that our folder contains four files named "First.java", "Second.java", "First.class" and "Second.class".
1. Create a JAR files using these four types of files.
Syntax:
jar -cf file_name.jar *<-. As shown in the following figure.
Here "cf" is used to create a JAR file, in which the "c" command creates a new archive and f specifies the archive filename.
"demo.jar" is our new JAR filename.
"*" is used to add all files in our demo.jar file.
Now open our folder and check that there is a new JAR file created with the name "demo.jar" that contains all the four files. As shown in fig.
2. View the components of a JAR file.
For viewing the components of a JAR file the syntax is:
jar -tf File_Name <-
Where "tf" shows the JAR file on the console in which the "t" commands shows a "list table of contents" and "f" specifies the archive filename. The command is shown below.
Now press Enter to view the components.
Note: You'll see two additional files shown there that is self-created by JAR tools. Don't worry about those files; they contain meta information about the file.
3. Extracting the contents of a JAR File.
To extract the component of JAR file the syntax are
jar -xf File_Name <-
Where "xf" extracts the JAR file. In which "x" is used to "extract the file" and "f" is used to specify the filename of the file we need to extract to.
Now delete all your files from the JAR folder except "demo.jar" and ensure that our folder contains only the single file named "demo.jar" as in the following:
Now enter the following command:
Now check our folder that the file was extracted using this command. as in the following:
Note:
Similarly we can perform another operation like updating (adding or deleting files from existing JAR file), adding classes, adding security, signing JAR files, etcetera.