Using the Jar Utility

Jar stands for Java Archive file and it is used to compress and archive one or more files. It is equivalent to Zip file in Window operating system. A typical jar file contains Java class files in addition to source files as well as resource files, like images and properties. Let us see how to create an Executable Jar file in this section. An executable jar file is nothing but a jar file along with a Manifest file containing the entry point of the Application specified in the form of class name.

also read:

For example, consider the following classes.

SimpleFrame.java

package tips.jar;
import javax.swing.JFrame;
public class SimpleFrame extends JFrame{
    public SimpleFrame(String title){
        super(title);
    }
    public void display(){
        this.setSize(600, 600);
        this.setVisible(true);
    }
}

The above class is a simple frame class that is responsible for creating a window with some title. Now, let us look into the following class which initiates and displays the window.

DisplayFrame.java

package tips.jar;
public class DisplayFrame {
    public static void main(String[] args) {
        SimpleFrame frame = new SimpleFrame("www.javabeat.net");
        frame.display();
    }
}

This class contains the main() method and we wish to provide this class name in the manifest file. Now, let us see how to create the executable jar comprising the above classes. Java comes up with an utility called jar.exe (located in JDK_INSTALLATION_PATHbin directory) which contains options for creating, listing, updating the jar file.

jar cvfm frame.jar Manifest.txt .tipsjarSimpleFrame.class .tipsjarDisplayFrame

In the above command, the options can be interpreted as follows,

C - to create the archive file
V – to give verbose output during the jar file creation
f – the name of the jar file, 'frame.jar'
m – to specify the manifest file 'Manifest.txt'

It is assumed that we have Manifest.txt file in the current directory and the class files (SimpleFrame.class and DisplayFrame.class) inside the tipsjar directory. It is necessary to specify the Manifest file (can be of any name with any extension) for creating an executable jar file, because it is the only place where we specify the entry-point of the application (i.e. the class with main() method). The content of the Manifest file is,

Main-Class: tips.jar.DisplayFrame

After running the jar command a file called frame.jar is created in the current directory. Now, to run the application we have to specify the '-jar' option like the following,

java –jar frame.jar

Now to list down the content of the jar file, we have to use the 't' option along with 'v' and 'f' options,

jar tvf frame.jar

The above command will list down the content of frame.jar (including directories and files).

0   Tue Aug 21 21:57:54 GMT+05:30 2007 META-INF/
103 Tue Aug 21 21:57:54 GMT+05:30 2007 META-INF/MANIFEST.MF
366 Tue Aug 21 21:52:00 GMT+05:30 2007 tips/jar/SimpleFrame.class
402 Tue Aug 21 21:52:00 GMT+05:30 2007 tips/jar/DisplayFrame.class

To extract the content of a jar file to the current working directory, we can use the 'x' option as used in the following command,

jar xvf frame.jar

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*