.jar files
by David Matuszek

A jar (Java ARchive) file is like a zip file or a tar file: it holds any number of compressed files. You usually need to uncompress ("un-jar") a jar file before you can use it.

Jar files are especially useful for applets. If an applet requires several classes, and maybe a picture or two, your computer has to ask for each part separately. Each part is a separate download. This is slow. However, you can put all the pieces of an applet into a single jar file, and a browser can download and use this file directly, which is much faster.

The usual HTML command for executing an applet is:

<applet code="MyMainClass.class" height="200" width="300">

But if you have everything in a jar file, this becomes:

<applet code="MyMainClass.class" archive="myJar.jar" height="200" width="300">

Creating a jar file from Forté:

Frankly, I think creating a jar file from Forté is too complicated, and you are better off doing it from the command line (see below). But if you want to, choose Help -> Contents from the main menu. In the window that appears, find Using the JAR Packager in the left pane, and click on the "key" to open it. Choose Creating a JAR File. Follow the directions you find there.

According to Dexter Hadley, if you create a jar file from Forté, it doesn't include your .java files. Since I require those files in order to grade your work, this would be a Bad Thing. I'm sure there must be a way to get Forté to include everything--this is an extra credit opportunity, if someone wants to figure it out and send me a clear writeup on how to do it.

Creating a jar file from BlueJ:

Choose Project -> Export... from the main menu. Select Store as jar file.

Choose a main class:

Make sure include source is checked (because we want to see your source code when we grade your program).

Click the Continue button.

Navigate to where you want to put the .jar file, type in a name for it, and click the Create button.

Creating a jar file from the command line:

This is the same for both MS-DOS and UNIX.

Make sure all the files you want to submit, and only the files you want to submit, are in one directory. (It may contain subdirectories, that's OK.) The name of the directory should not contain spaces or other odd characters.

Go to the directory containing this directory. If you are on UNIX, you should see the name of the directory when you do a ls command. If you are on Windows, you should see the name of the directory when you do a dir command. Jar this one directory. If the name of your directory is myAssignment, and you want to name the created jar file myAssignment.jar, then the command would be:

jar cvf myAssignment.jar myAssignment

There are three commands you should know:

jar cvf myJar.jar file1 file2 ... fileN
Creates a file myJar.jar containing all the files file1 through fileN. You can use wildcards, for example, jar cvf myJar.jar *.java *.class *.html will jar up all your Java source files, class files, and HTML files. For this course, you normally should use only one file name, the name of a directory containing all the files you are to hand in.

jar tvf myJar.jar
Lists the contents of the jar file.

jar xvf myJar.jar
Extracts the contents of the jar file.

If Java is installed completely correctly, the above commands will work fine. If not, you will get an error message such as

'jar' is not recognized as an internal or external command,
operable program or batch file.

In this case you will have to use the complete path to the jar program, which is in Java's bin directory. For example, on my PC I need to use a command such as:

C:\>jdk1.3.1_01\bin\jar cvf Fibonacci.jar *.java *.class *.html readme.txt

Note: to get a command line on Windows, open the MS-DOS program. If you don't appear to have this program, choose Start -> Run... and type in cmd.