JAR Files Core Java

When you package your application, you want to give your users a single file, not a directory structure filled with class files. Java Archive (JAR) files were designed for this purpose. A JAR file can contain both class files and other file types such as image and sound files. Moreover, JAR files are compressed, using the familiar ZIP compression format.

TIP: Java SE 5.0 introduced a new compression scheme, called “pack200”, that is specifically tuned to compress class files more efficiently than the generic ZIP compressionalgorithm. Sun claims a compression rate of close to 90% for class files.

You use the jar tool to make JAR files. (In the default JDK installation, it’s in the jdk/bin directory.) The most common command to make a new JAR file uses the followingsyntax:

Table below lists all the options for the jar program. They are similar to the options of the UNIX tar command.

jar Program Options

jar Program Options

You can package application programs, program components, and code libraries into JAR files. For example, the runtime library of the JDK is contained in a very large file rt.jar.

The Manifest

In addition to class files, images, and other resources, each JAR file contains a manifest file that describes special features of the archive. The manifest file is called MANIFEST.MF and is located in a special META -INF subdirectory ofthe JAR file. The minimum legal manifest is quite boring —just Manifest -Version: 1.0

Complex manifests can have many more entries. The manifest entries are grouped into sections. The first section in the manifest is called the main section. It applies to the whole JAR file. Subsequent entries can specify properties of named entities such as individual files, packages, or URLs. Those entries must begin with a Name entry. Sections are separated by blank lines. For example: Manifest -Version: 1.0

To edit the manifest, place the lines that you want to add to the manifest into a text file. Then run

To update the manifest of an existing JAR file, place the additions into a text file and use
a command such as

Executable JAR Files

As of Java SE 6, you can use the e option of the jar command to specify the entry point of your program—the class that you would normally specify when invoking the java program launcher:

In older versions of the JDK, you had to specify the main class of your program, following this procedure:

Do not add a .class extension to the main class name. Then run the jar command:

CAUTION: The last line in the manifest must end with a newline character. Otherwise, the manifest will not be read correctly. It is a common error to produce a text file containing just the Main-Class line without a line terminator.

Depending on the operating system configuration, you may be able to launch the application by double-clicking on the JAR file icon. Here are behaviors for various operating systems:

  • On Windows, the Java runtime installer creates a file association for the “.jar” extension that launches the file with the javaw -jar command. (Unlike the java command, the javawcommand doesn’t open a shell window.)
  • On Solaris, the operating system recognizes the “magic number” of a JAR file and starts it with the java -jar command.
  • On Mac OS X, the operating system recognizes the “.jar” file extension and executes the Java program when you double-click on a JAR file.

However, a Java program in a JAR file does not have the same feel as a native application. On Windows, you can use third-party wrapper utilities that turn JAR files into Windows executables. A wrapper is a Windows program with the familiar .exe extension that locates and launches the Java virtual machine (JVM), or tells the user what to do when no JVM is found. There are a number of commercial and open source products, such as JSmooth. The open source installer generator IzPack also contains a native launcher.

On the Macintosh, the situation is a bit easier. The application package utility MR JApp Builder lets you turn a JAR file into a first -class Mac application.


Classes that are used in both applets and applications often use associated data files, such as

  • Image and sound files;
  • Text files with message strings and button labels; or
  • Files with binary data, for example, to describe the layout of a map.

In Java, such an associated file is called a resource.

NOTE: In Windows, the term “resource” has a more specialized meaning. Windows resources also consist of images, button labels, and so on, but they are attached to the executable file and accessed by a standard programming interface. In contrast, Java resources are stored as separate files, not as part of class files. It is up to each program to access and interpret the resource data.

For example, consider a class, AboutPanel, that displays a message such as the one in

Displaying a resource from a JAR file

Displaying a resource from a JAR file

But where should you put a file such as about.txt? Of course, it would be convenient if you simply placed it with the rest of the program files inside the JAR file.

The class loader knows how to search for class files until it has located them somewhere on the class path, or in an archive, or on a web server. The resource mechanism gives you the same convenience for files that aren’t class files. Here are the necessary steps:

  1. Get the Class object of the class that has a resource, for example, AboutPanel.class.
  2. If the resource is an image or audio file, call getResource(filename) to get the resource location as a URL. Then read it with the getImageor getAudioClipmethod.
  3. For resources other than images or audio files, use the getResourceAsStreammethod to read the data in the file.

The point is that the class loader remembers how to locate the class and it can then search for the associated resource in the same location.

For example, to make an icon with the image file about.gif, do the following:

That means “locate the about.gif file at the same place where you find the Resource Test

class.” To read in the file about.txt, you use these commands:

Instead of placing a resource file inside the same directory as the class file, you can place it in a subdirectory. You can use a hierarchical resource name such as

This is a relative resource name, and it is interpreted relative to the package of the class that is loading the resource. Note that you must always use the / separator, regardless of the directory separator on the system that actually stores the resource files. For example, on the Windows file system, the resource loader automatically translates / to separators.

A resource name starting with a / is called an absolute resource name. It is located in the same way that a class inside a package would be located. For example, a resource /corejava/title.txt is located in the corejavadirectory (which may be a subdirectory of the class path, insidea JAR file, or, for applets, on a web server).

Automating the loading of files is all that the resource loading feature does. There are no standard methods for interpreting the contents of a resource file. Each program must have its own way of interpreting the contents of its resource files.

Another common application of resources is the internationalization of programs. Language-dependent strings, such as messages and user interface labels, are stored in resource files, with one file for each language.

Listing below shows the source code of the program that demonstrates resource loading.

Move the JAR file to a different directory and run it again to check that the program reads the resource files from the JAR file, not the current directory.



  • URL getResource(String name)
  • InputStreamgetResourceAsStream(String name)
    finds the resource in the same place as the class and then returns a URL or inputstream you can use for loading the resource. Returns null if the resource isn’tfound, and so does not throw an exception for an I/O error.


We mentioned earlier that you can seal a Java language package to ensure that no further classes can add themselves to it. You would want to seal a package if you use package-visible classes, methods, and fields in your code. Without sealing, other classes can place themselves into the same package and thereby gain access to its packagevisiblefeatures.

For example, if you seal the package com.mycompany.util, then no class outside the sealed archive can be defined with the statement package com.mycompany.util;

To achieve this, you put all classes of the package into a JAR file. By default, packages in a JAR file are not sealed. You can change that global default by placing the line Sealed: true into the main section of the manifest. For each individual package, you can specifywhether you want the package sealed or not, by adding another section to the JAR filemanifest, like this:

To seal a package, make a text file with the manifest instructions. Then run the jar command in the usual way:

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Core Java Topics