JAR Files




Instead of having to type in a command like 'java -classpath c:\code Tedious' evey time you wanted to run a program, it would be a lot handier if you could just double click on a file. Fortunatley JAR files let us do just that as well as providing a host of other features. This article should have you creating and running your own JAR files without any hassle.

This article deals with JDK 1.2 or later only.

JAR files, eh?

Yes. Java archive files or JAR files as they're usually known provide all the benefits of lossless data compression, archiving and executability. A JAR file puts all the files that your program needs - class, image, audio, whatever - into one JAR file. It also maintains the directory structure of the files that are archived. Creating a JAR file also means that if someone is downloading your program all the files your program needs can be downloaded in one http transfer. This and the fact that the files are compressed considerably speeds up the download time, particularly for larger programs.

Although you can run a JAR file by double-clicking on it, this doesn't mean that JAR files are self-executable. If a file is self-executable it relies on the hardware on which it's runing. JAR files rely on the Java Runtime Environment being installed on the system.

Some background

Creating, running and unpacking JAR files are all relatively simple. JAR files offer a lot functionality than I'm not going into here, such as electronic signing. If you want to learn all about JAR files take a look at Sun's Java tutorial. What I'm convering here should be enough to get you creating and running your own JAR files.

For the sake of this article, I'm going to use a program that's a slightly modified version of the application presented on the running applets and applications page. Download these three java files and save them to an empty directory.

TestApp.java
DemoPanel.java
PreLabel.java

We're going to create a JAR file called TestJar.jar that will contain the compiled bytecode of these three files. Compile these three files - there should now be seven files in your directory - three source files and four class files. There are four class files because TestApp.java contains an anonymous inner class - this anonymous inner class is compiled to create its own class file, TestApp$1.class .

The nitty gritty

So you've got all your source and class files in the one directory. The easiest way to go about creating your JAR is to open up a DOS prompt and cd to your directory. If you're using some other flavour of OS you can specify the full path from the command line.

To create your JAR you type:

jar cf jar-filename input-file(s)  in our case 
jar cf TestJar.jar DemoPanel.class PreLabel.class TestApp.class TestApp$1.class
You can use wildcards for the input-file parameter so typing
jar cf jar-filename *.class  or in our case 
jar cf TestJar.jar *.class
will add every class file in that directory to your JAR file. By the way the 'c' stands for create.

So you've made your JAR file, you want to take a look at what's in it. Simply type:

jar tf jar-filename  for us 
jar tf TestJar.jar 
That will provide you with a table - hence the 't' - listing all the files in the JAR file you've just created plus one more - the manifest file, but more on that later.

Run that JAR

Running JAR's as applets is very straighforward. For this example a JAR file would have been created using the TicTacToe applet provided in the JDK demo directory. Let's say the html code for the applet looks like this:

<applet code=TicTacToe.class
        width=120 height=120>
</applet>
If you create a JAR file you have to specify the archive name. You still need the to specify the code so your browser knows where to find the class that contains init(), start() and paint() . So the new html code will look like this:
<applet code=TicTacToe.class
        archive="TicTacToe.jar"
        width=120 height=120>
</applet>
Remember if you're running a Swing applet, you'll need to use the HTML Converter for it to run properly in a browser.

Running a JAR file as an application requires a little more work. The manifest file that I mentioned earlier can contain some extra information about your JAR file, build version, signatures and so on. If you're running a JAR file as an application, the manifest file will have to know which class file contains the main method. So create a text file containing this one line:

Main-Class: main-class-name or for us
Main-Class: TestApp

Note: Make sure that your text file ends with a new line or carriage return, if it doesn't the last line of your text file won't be parsed properly.
Save this text file as mainclass.txt .

Now we're going to have to update the manifest file with your new manifest file. So type:

jar umf mainfest-file-name jar-filename  in our example

jar umf mainclass.txt TestJar.jar
The 'u' stands for update and the 'm' for manifest. You're all ready to run your jar file now. You can do that by simply double clicking the file's icon, or you can type:
java -jar TestJar.jar

Troubleshooting

Menu
Last updated August 15th 2000