In the Java world, if there is any file format that everybody is familiar with, it is JAR files. JAR files are an archive or a collection of files distributed as a single unit with a .jar file extension. Archiving is like putting all of your Java and other resource files into a single case and getting them ready to be shipped as a .jar file (other archive extensions exist as well). This programming tutorial covers the concept behind the JAR file format and gives a quick introduction on how to work with .jar files in Java.
What is JAR?
JAR stands for Java Archive. It is a platform independent file format specifically used to compress and bundle multiple files into a single archive called a JAR file. The compressing technique used is based on the popular ZIP file format. Although JAR can be used as an generic archiving tool, it was primarily developed to download an aggregate of files in the browser in a single HTTP transaction. This was the scenario back when Java Applets were in vogue and the class, images, and sound files were downloaded as an HTTP request and hosted by the browser.
Back in the days it had a finer impact on performance and web pages quickly became responsive with the downloaded applets. Since file compression is imbibed into the JAR file, it reduces file size and, consequently, has a shorter download time. Another aspect is that each of the JAR files can be digitally signed to authenticate its origin.
Archiving Files in Java with JAR
JAR is still a popular file archive format, at least in the Java arena, and is used extensively for many different purposes. Some of the advantages of JAR files include:
- JAR files are a cross-platform archive format
- JAR files can archive a wide variety of file types, be they classes, audio files, images, or text-based
- JAR files are backwards compatible
- Almost all developers prefer JAR files, making it an obvious choice for most scenarios involving archiving files in the Java world
In a typical scenario, applications developed in Java consist of many source files. Once compiled, object code – or .class files – are created for each public class or interface. These files, when transmitted over a network, say, in an HTTP protocol request which needs separate socket connections for each file transmission, may be very large; .class files, for instance, may be a few hundred bytes in size. Therefore, the time required to make every socket connection and disconnection, respectively, for each file is just a waste of time.
Now consider this scenario: all the files are JAR archived, compressed using the PKZIP algorithm, and distributed as a single unit. The performance of this transfer would be completely different than our previous scenario. This would represent significant improvement on the overall performance of the application, because JAR files are now received as a single unit, which then can be uncompressed into their original form as per the program requirements at the receiving end. This is actually the classic reason for the existence of JAR files in the Java Applet days.
Use Cases for JAR Files in Java Applications
p>Java Applets may be outdated, but their companion libraries are alive and well. JAR files are among them. It is convenient to bundle libraries in a JAR archive and, as we can see, most Java libraries come in a bundle of JAR files. Developers can make a fat-jar by bundling all class files into a single archive for easy distribution. However, this is discouraged. Instead, compiling a leaner, cohesive file into a separate archive is recommended. This separation of files into smaller units of concern not only leverages storage but also leverages minor upgrades on the part of a library which can leave other unconcerned files undisturbed.
What Are Executable JAR Files
Programmers can package executable Java programs into a JAR file along with libraries, images and other files it uses. Developers can simply execute the JAR file in a click-and-run fashion. The executable JAR file keeps a manifest file specifying the class path and the entry point of the application, which is nothing but the class that contains the main method: Main-Class: App.MainClass. Some operating systems allow it to run on a click; others use a simple command line invocation:
$ java -jar DemoApp.jar
How to Create a JAR File in Java
The Java Development KIT (JDK) ships a .jar tool to package Java code into a JAR file format. Starting with JDK9, JAR has been enhanced to work with modules as well, but in this tutorial, we will focus on the basic functionalities of working with JAR tools. Note that, once JDK is installed, programmers are equipped to work with JAR files. The basic command for creating a JAR file is as follows:
$ jar cf jar-file input-file(s)
Here, the option c indicates that we want to create a JAR file and f indicates that we want the output to go to a file. Now, suppose we have three files: a.txt, b.class, and c.jpg. If we wanted to create a JAR file named demo-jar, we would use the command:
$ jar cf demo-jar a.txt b.class c.jpg
Using this command would create a demo-jar JAR file. Note that a JAR file can actually have any – or no – extension. If we want a specific .jar extension, we can rewrite the above command as follows:
$ jar cf demo-jar.jar a.txt b.class c.jpg
Once the JAR file is created, the input files are compressed and can be distributed as a single unit: demo-jar.jar in this case.
How to View JAR Content
Now, after creating a JAR file, we may want to view the contents of the JAR file. The basic command to do that is as follows:
$ jar tf demo-jar.jar
This shows a listing similar to the following, dependent upon the file names in the JAR:
META-INF/ META-INF/MANIFEST.MF a.txt b.class c.jpg
Note that, apart from the three files we have archived, the JAR file also contains MANIFEST.MF inside the META-INF folder/directory. This is automatically generated by the jar command. The file contains a list of name-value pairs, separated by colons and grouped into sections.
If the JAR file is only to be used for archiving, then this file is not of much use. If an application is to be bundled in the JAR file, then this file must contain the entry point for the Java Virtual Machine (JVM) to run the program.
The entry point refers to the class that contains the main method. A JAR file that is to be used for download contains the list of files and their classpath information. The JAR file that we have created is pretty basic and contains the following information only. The MANIFEST.MF file is a simple text file and can be opened by any text editor:
Manifest-Version: 1.0 Created-By: 19.0.1 (Oracle Corporation)
How to Extract a JAR File
A JAR file can be extracted with the following command:
$ jar xf demo-jar.jar
When extracting the JAR tool, it creates a copy of the files in the current directory; the original JAR file remains unchanged. The extraction overwrites any files having the same name in the current directory and pathname.
How to Update a JAR File
Developers can update or add new files to the existing JAR file using the following command:
$ jar uf demo-jar.jar d.class
Care should be taken in adding new files into existing archives because any file in the archive with the same name will be silently overwritten.
Final Thoughts on Working with Java JAR Archive Files
There are a plethora of options available when working with the JAR tool. A simple jar –help command can give a quick overview of these options. As a Java programmer, it is not possible that one has not dealt with JAR tools – either directly or indirectly.
There is another file format called WAR (Web Archive) used for bundling Java web applications and EAR (Enterprise Archive) used for archiving enterprise applications composed of multiple modules. These are special extensions of the JAR format, but, unlike JAR, EAR and WAR files cannot be run as standalone applications.