How to work with Java JAR files

Guides for Java Developers

In the Java world, if there is one file format that everyone is familiar with, it is JAR files. JAR files are an archive or collection of files distributed as a single unit with .jar file extension. Archiving is like putting all your Java and other resource files into one box and preparing them to be shipped as .jar files (there are other archive extensions). This programming guide covers the concept behind the JAR file format and provides a quick introduction to working with it .jar files in Java.

Read: The best online courses to learn Java

What is a JAR?

JAR is an abbreviation for Java Archive. It is a platform-independent file format specifically used to compress and group multiple files into a single archive called a JAR file. The compression technique used is based on the popular ZIP file format. Although a JAR can be used as a generic archiving tool, it was primarily developed to download a set of files to a browser in a single HTTP transaction. This was the scenario when Java Applets were in vogue and class, images and sound files were downloaded as an HTTP request and hosted in the browser.

It used to have a better impact on performance and websites responded quickly with downloaded widgets. Since file compression is built into the JAR file, it reduces the file size and, consequently, has a shorter download time. Another aspect is that each of the JAR files can be digitally signed to verify 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 various types of files, whether they are classes, audio files, images or text files
  • JAR files are backwards compatible
  • Almost all developers prefer JAR files, making them the obvious choice for most scenarios involving file archiving in the Java world

In a typical scenario, applications developed in Java consist of many source files. After compilation, object code – or .class files – are created for each public class or interface. These files, when transferred over the network, say in an HTTP protocol request that requires separate socket connections for each file transfer, can be very large; .class files, for example, can be several hundred bytes in size. So the time it takes to connect and disconnect each socket for each file is just a waste of time.

Now consider this scenario: all files are JAR archived, compressed using the PKZIP algorithm, and distributed as a single unit. The performance of this transfer would be completely different from our previous scenario. This would represent a significant improvement in overall application performance, as the JAR files are now received as a single unit, which can then be decompressed to its original form as required by the program on the receiving end. This is actually the classic reason for the existence of JAR files in the Java Applet days.

Read: Java tools to increase productivity

Use cases for JAR files in Java applications

p>Java Applets may be outdated, but their supporting libraries are alive and well. JAR files are among them. It is convenient to group libraries into a JAR archive and, as we can see, most Java libraries come in a package of JAR files. Developers can make a fat-jar by merging all class files into one archive for easy distribution. However, this is not recommended. Instead, it is recommended to compile a smaller, cohesive file into a separate archive. This separation of files into less important units not only increases storage, but also affects minor upgrades in a part of the library that may leave other uninteresting files undisturbed.

What are executable JAR files

Developers can package executable Java programs into a JAR file along with the libraries, images, and other files it uses. Developers can easily execute the JAR file in a click-and-run manner. An executable JAR file stores a manifest file specifying the classpath and application entry point, which is nothing but the containing class main method: Main class: App.MainClass. Some operating systems allow click-to-run; others use a simple command line invocation:

$ java -jar DemoApp.jar

How to create a JAR file in Java

The Java Development KIT (JDK) provides a .jar tool for packaging Java code into a JAR file format. Starting with JDK9, JAR has also been improved to work with modules, but in this tutorial we will focus on the basic functions of working with JAR tools. Note that once the JDK is installed, developers are equipped to work with JAR files. The basic command to create a JAR file is as follows:

$ jar cf jar-file input-file(s)

Here’s an 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. classand c.jpg. If we want to create a JAR file named demo-jarwe 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 extension or no extension at all. 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 the JAR file, we may want to look at the contents of the JAR file. The basic command for this is as follows:

$ jar tf demo-jar.jar

This shows a list similar to the following, depending on the filenames in the JAR:

META-INF/
META-INF/MANIFEST.MF
a.txt
b.class
c.jpg

Note that in addition to the three files we archived, it also contains a JAR file MANIFEST.MF within META-INF folder/directory. This is automatically generated jar command. The file contains a list of name-value pairs, separated by colons and grouped into sections.

If the JAR file is only used for archiving, then this file is not very useful. If the application is to be added to a JAR file, that file must contain entry point for the Java Virtual Machine (JVM) to run the program.

An entry point refers to the containing class main method. The JAR file used for the download contains a list of files and their classpath information. The JAR file we created is quite simple and only contains the following information. The MANIFEST.MF file is a simple text file and can be opened with any text editor:

Manifest-Version: 1.0
Created-By: 19.0.1 (Oracle Corporation)

How to extract the JAR file

The 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 all files that have the same name in the current directory and pathname.

How to update a JAR file

Developers can update or add new files to an existing JAR file using the following command:

$ jar uf demo-jar.jar d.class

Care should be taken when adding new files to existing archives as any file in the archive with the same name will be silently overwritten.

Final thoughts on working with Java JAR archive files

There are tons of options available when working with a JAR tool. Easy jar – help command can provide a quick overview of these options. As a Java developer, it is not possible that you have not dealt with JAR tools – either directly or indirectly.

There is another file format called WAR (Web Archive) used to bundle Java web applications and EAR (Enterprise Archive) is used for archiving business applications composed of several modules. They are special extensions of the JAR format, but unlike JAR, EAR, and WAR files, they cannot be run as standalone applications.

Read more Java programming tutorials and software development tips.

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *