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