Definition

JAR file (Java Archive)

What is a JAR file (Java Archive)?

A Java Archive, or JAR file, contains all of the various components that make up a self-contained, executable Java application, deployable Java applet or, most commonly, a Java library to which any Java Runtime Environment can link.

There are two key benefits of using a JAR file. The first is the ability to aggregate hundreds, if not thousands, of different files that make up an application. The second is the means to compress all of the contained files, greatly reducing the size of the application, and making it easier to move the JAR file over a network and between environments.

Contents of a JAR file

Java applications and libraries can contain hundreds of different files, including compiled Java source code, a manifest file, XML-based configuration data, JSON-based data files, images, sound clips and even security certificates. A JAR file is simply an aggregation of all of these resources into a single, compressed file.

Since a JAR file uses a standard compression algorithm, opening a JAR file is as simple as changing its extension from .jar to .zip and extracting the contents using a standard decompression tool.

Opening a JAR file and exploring the contents
Opening a JAR file and exploring the contents

Although optional, it is recommended that JAR files contain a manifest file named MANIFEST.MF in a folder named META-INF. This manifest file contains metadata about the JAR, including properties such as the code version, the primary author of the code and the name of the organization that maintains the code.

Executable JAR files

For an executable JAR file that contains a stand-alone application, a Main-Class attribute should exist that provides the name of the first piece of code for the Java Runtime Environment (JRE) to invoke when the application is run.

Manifest file MANIFEST.MF for the open source hibernate3.jar file
Manifest file MANIFEST.MF for the open source hibernate3.jar file

Java Archive apps

Each installation of the Java Development Kit (JDK) includes a JAR utility (named jar.sh on Unix and jar.exe on Windows) that provides a variety of helpful functions for working with JAR files, including the ability to:

  • create new JAR files with a manifest file;
  • extract all of the contents of a JAR file onto the file system;
  • update an existing JAR file by adding files to it; and
  • update a JAR file's existing manifest file

JAR file openers

To open a JAR file and extract the contents to the file system, you must use two JAR utility switches, namely, "x" to extract the contents, and "f" to specify the name of the JAR file being opened. The utility then extracts the contents to the file system.

For example, the JAR utility command to extract the contents of a JAR file named my_java_app.jar looks as follows:

> jar xf C:\techtarget\my_java_app.jar

The Java JAR command

The JAR utility can also help create a JAR file. To create a JAR file named tss.jar that includes two files named Tech.class and Target.class, the command looks as follows:

> jar cf tss.jar Tech.class Target.class

How to run a JAR files

Any Java code that has a method called "main" is considered to be a stand-alone application. For example, the code below has a main method that triggers a dialog box to pop up that says "Hello World" in it.

package theserverside;
import javax.swing.*;

public class HelloWorldApplication {    
    

  public static void main(String args[]){

    String title = "Executable Java Application";

    String message = "Hello World!";

    JOptionPane.showMessageDialog(null, message, title, 1);

  }

}

If this code was packaged in a JAR file, the JAR file would execute it because it contains code that can be run directly by the JRE. However, to run the executable JAR file, you don't use the JAR utility of the JDK. Instead, you use the special java.exe utility, and, thus, the name of the jar file is specified.

The syntax to run an executable JAR file named my_java_app.jar using the JDK's java.exe utility is as follows:

> java.exe -jar my_java_app.jar

This was last updated in October 2021

Continue Reading About JAR file (Java Archive)

Dig Deeper on Development tools for continuous software delivery

App Architecture
Software Quality
Cloud Computing
Security
SearchAWS
Close