Java ARchive (JAR) Files 101

Wednesday Sep 30th 2009 by Sridhar M S

JAR is the Java way of compressing a file. Learn how to use some basic and advanced features of this fundamental tool.

If you work with Java, you have come across JARs (or Java Archives). Java SE API supports working with JAR files, and the jar tool, which is available as part of the JDK, supports all JAR-related development. Similar to the more common ZIP format, JAR is the Java way of compressing a file. Because JARs actually use the ZIP format for compression, they are compatible with other applications as well.

Compressed files are used so frequently because they download easily and require less disk space (although disk space is not a problem these days). JAR files support these fundamental ideas of compression with some other value-added features, including versioning and security. Some of the basic features are the abilities to create, update, and view JAR files. The advanced features include using the JAR files and understanding Manifest files. This article delves into the advanced features, but so as not to ignore the basics, the following section walks you through some of the basic features.

Basic JAR Commands and Features

Type jar at the command prompt and it lists down all the available options. Here are some of the options and their descriptions:
  • c: Create, indicates that a file needs to be created
  • t: Table of contents
  • f: File, the output will be a file
  • v: Verbose, lists all the activities that the tool is performing
  • m: Merge information from an external file to the manifest file (More about manifest files further in this article)

These options are the most widely used. When you use them with the jar command, they produce the desired results. For example, the following command uses a few options and creates a JAR file named <jar-file-name>, taking inputs from the <input-file(s)> argument:

jar  –cvf  <jar-file-name>  <input-file(s)>
If you have the following folder structure:
|            |__mycomp
|                       |____demo
|                                   |

Executing the command jar cvf MyApplication.jar * would result in the following output:

added manifest
adding: com/(in = 0) (out= 0)(stored 0%)
adding: com/mycomp/(in = 0) (out= 0)(stored 0%)
adding: com/mycomp/demo/(in = 0) (out= 0)(stored 0%)
adding: com/mycomp/demo/ = 0) (out= 0)(stored 0%)
adding: demo/(in = 0) (out= 0)(stored 0%)
adding: images/(in = 0) (out= 0)(stored 0%)
adding: lib/(in = 0) (out= 0)(stored 0%)

The output is the file MyApplication.jar, which has all the folders compressed into a single file. The most important thing to notice here is the new folder created in the JAR file: META-INF, which contains a MANIFEST.MF file.

To view the contents of the JAR file, use the command jar tf MyApplication.jar. The result will be the following output:


To update the JAR file, you can use the command jar uf MyApplication.jar <new-file(s)>.

JAR File in App Deployment

Now that you have a JAR file, you may want to use it as part of your application deployment process. Typically, you will form many JARs for different application components (client and server, for instance) and use them in an application.

The class, which is bundled in the JAR file, is only an example. For actual deployments, you would bundle only the .class files instead of the .java files. Assuming that you have now bundled the MyApplication.class file, you can execute this class file. The java command has an option for this.

The command java –jar –MyApplication.jar would result in the following output:

Failed to load Main-Class manifest attribute from

For this command to work as expected, you need to do one important thing. The MANIFEST.MF in the MyApplication.jar file has to be updated with the following line:

Main-Class: com.mycomp.demo.MyApplication

Now the modified MANIFEST.MF file will have the following contents:

Manifest-Version: 1.0
Created-By: 1.5.0_19 (Sun Microsystems Inc.)
Main-Class: com.mycomp.demo.MyApplication

Also, in the Java code download, the file is updated as follows with the string main method of MyApplication:

package com.mycomp.demo;
public class MyApplication
    public static void main(String args[])
        System.out.println("main method of MyApplication.");

The addition of the main class is also referred to as defining the entry point for the JAR file.

To update your MyApplication.jar file, repackage the class file and the updated MANIFEST.MF file. Now, execute the command java –jar –MyApplication.jar to get the desired result.

More About Manifest File

Manifest is a simple text file that contains all the directions for how the JAR file will behave. When you used the jar command and created MyApplication.jar, a default MANIFEST.MF file was created under the META-INF folder. This is the default folder in which the MANIFEST.MF file is placed.

The MANIFEST.MF file contains the version of the manifest specification and information about the JDK in which it was created (see below). These are standard details that will be part of all manifest files.

Contents of the manifest file
Manifest-Version: 1.0
Created-By: 1.5.0_19 (Sun Microsystems Inc.)

Remember the line Main-Class: com.mycomp.demo.MyApplication that you added to the manifest file earlier? You can do this by either editing the manifest file in the JAR or by using commands.

If you are not comfortable editing the manifest file yourself, the jar tool provides an option (m) to achieve this. So, simply modifying your create-JAR-file command to jar –cmvf <jar-file-name> manifest-inclusion-file <input-file(s)> will result in the creation of a JAR file, in which the manifest file will be updated with the contents in manifest-inclusion-file. Remember that you need to have a new line at the end of the file, or the manifest file will not be created as expected. In this case, the command would be jar –cmvf MyApplication.jar main-class-file.txt *.

Contents of the main-class-file
Main-Class: com.mycomp.demo.MyApplication
<This is a new line, which is necessary for the parser to parse this file.>

In addition to all this great stuff, Class-Path is one of the most widely used metadata attributes in manifest files. Like any Java class path, you can extend Class-Path to be part of a JAR file too. The syntax is similar to Main-Class and, in this case, it is Class-Path: <file-name1> <file-name2>. You can place this in a file similar to your main-class-file.txt or in the same file and the manifest update command will do the job for you. By definition, class paths are essential for your application to locate different classes that it will use during its execution.

Code Download

  • Java
  • For Further Reading

  • "jar-The Java Archive Tool" (from
  • About the Author

    Sridhar M S is a Java developer from Bangalore, India. He holds a master's degree in Computer Science.

    Mobile Site | Full Site
    Copyright 2017 © QuinStreet Inc. All Rights Reserved