Using jar

This page is a guide on how to use the JAR program which is part of the Java SDK. It assumes you are on a Windows system, because that is what I usually use and that is what is installed in the MicroLab. If your system is Linux I assume you can figure out how to use jar on your own. I don't use Macs much but I suspect the procedures are similar except for opening a window with a command prompt. (Apple's page on jar. Apple's reference page for Java in general. A series of articles on using the Terminal program on Macs. Pick the one based on the version of Mac OS X you have) 

**When learning jar it is very easy to overwrite your source files. Be sure to back up your files in a safe location before trying to create a jar file in case you accidentally overwrite while learning to use jar.exe! **

JAR files are the output of the Java Archive Tool. The Java Archive Tool is a program that is part of the Java Standard Edition Software Development Kit.). It is loaded on the computers in the Microlab and if you download Java onto your computer, the Java Archive Tool is one of the is included. The programs that are part of the Java Software Development Kit are stored in a folder or directory called bin. (Sidetrack: Why is it called bin? bin is short for binaries. Binaries is another term for executable programs. The compiled version of source code. They contain the machine language instructions the CPU understands.) The bin folder is in turn located in whatever folder or directory you put Java in when you installed it. For example, on my computer at home the Java SDK is located in the directory C:\j2sdk1.4.2_01. (I have not gotten around to installing Java 5.0 on my home computer.) It is interesting to look at all the programs that make up the SDK that are stored in the bin directory. 

Note the programs javac (the Java compiler), java (the Java runtime system), javadoc (the program that creates those beautiful html documentation pages from source code), and jar

The easiest way to run the Java Archive Tool and create jar files is from a command prompt. Just clicking on the jar icon won't work, because you have to specify certain information. If you are using windows you can open a command prompt by going to the start menu,  choosing run, typing cmd, and pressing return. This will open a command window.

Clicking run opens a window similar to this on a Windows machine.

Pressing okay opens a command window or "dos prompt" like this.

If you are under the age of 25 you may have never used an operating system in this way. In the above window the operating system is waiting for a command. Instead of pointing and clicking commands are issued by typing them in. Many programmers who use Unix or Linux do most of their work with the operating system via a textual interface instead of a graphical one. (And sometimes both.) For a detailed run down of what you ca do at the Windows command prompt see this web page. (XP version.) 

I think the most frustrating thing about working from the command prompt is dealing with file names and path. Let's say we want to create a jar file named A2.jar which contains the and classes. The command prompt above is currently located in the C direcotry. Thus the C:\ at the command prompt. The syntax for creating jar files as seen on Sun's page for jar is:

jar cf A2.jar

The c and the f are arguments to the jar program indicating we want to create a jar file and we want the output of the program to go to a file rather than standard output. Next comes the name of the file to create, A2.jar in this case. Finally a list of the files to include in the jar file, and in this case. What happens when we try this command at the prompt?

My operating system doesn't understand the command jar. There are two ways to fix this. The first fix is to type in the full path name of the jar file. On my system at home this would be C:\j2sdk1.4.2_01\bin\jar On your system it will be wherever the jar program is actually located. Let's see what happens when we try this.

Now the jar program was found, but and were not found. Will get to that problem in a second. The second way to fix the problem of the operating system not knowing what jar is, is to change the path. Generally the path is where the OS looks to run programs. If you type path at the command prompt you will see what directories the OS is looking in to run programs. 

You can add directories to the path. Look at the installation notes from Sun for the Java SDK to see how to permanently add a path in Windows.

Once we were able to specify to the OS where the jar was, either by using its full path name or changing the default paths the OS looks in, we still need a way to specify which files to include in the jar. When we were at the top level directory and were not found. That is because they are buried down in a folder. So we could type the full path name of these files or we could move to the folder that contains the files. 

Use the cd (change directory) command to move to the directory that contains the files you want to archive. 

This could have been done in one step by typing the command cd Mike's Stuff\cs307\Flesch . The command dir displays the contents of a directory. Depending on how your version of Windows is set up it may have problems dealing with directories names that contains spaces. There are two ways to work around this. First you can put the name of the directory or path in double quotes. For example cd "Mike's Stuff\cs307\Flesch" . The second work around is to turn off command extensions. With command extensions on spaces are used as delimiters and so spaces in directory names are interpreted as specify a different part of a command. (Such as cd.) To turn off command extensions so that spaces are not treated as delimiters type cmd e:off .

Once you have navigated to the directory that contains the files you want to include in the JAR file use the JAR command to create the file. Assuming I didn't change the path and had to type the full location of jar, my command would look like this:

Notice there wasn't at feedback when the program was run. When I type the command dir to view the contents of the directory A2.jar is now present. If you want to check the contents of the jar you can either "unjar" it.

To check that you have jarred the files correclty move the jar file out of your code development folder. Unjar it with the following command

jar xf jar-file

Where jar-file is the name of the jar file. You can now look at what you had in the jar to esnure they are the correct files.

When you do this be sure you don't unjar the file in the same directory as the original because you will overwrite them. See Sun's page on extracting jar files. The other way to look at the contents of the jar is a bit of a hack. jar area actually created with the ZIP file format. So you could also change the extension on your jar file to .zip and use a zip program to look at the contents of the jar. Then change the extension back to jar when you are satisfied you have the correct files.  

BlueJ and Eclipse provide the capability to produce JAR files. See the respective documentation for the details. (BlueJ reference manual or Eclipse reference manual for Java Development Tools. The previous link takes you to the Eclipse Java Development Tools homepage. Click on the link to documentation on the lower left and then click on the link to the Java Development Users Guide.) The BlueJ version of jar is under the Project menu in BlueJ. This feature was designed to package up BlueJ projects and includes a lot of extra garbage we don't want and does not preserve the directory structure.

The following was copied from the Eclipse Documentation:

Creating a new JAR file

To create a new JAR file in the workbench:

  1. In the Package Explorer, you can optionally pre-select one or more Java elements to export. (These will be automatically selected in the JAR Package Specification wizard page, described in Step 4.)
  2. Either from the context menu or from the menu bar's File menu, select Export.
  3. Select JAR file, then click Next.
  4. In the JAR Package Specification page, select the resources that you want to export in the Select the resources to export field.
  5. Select the appropriate checkbox to specify whether you want to Export generated class files and resources or Export Java source files and resources. Note: Selected resources are exported in both cases. For CS307 you want to export the source files.
  6. In the Select the export destination field, either type or click Browse to select a location for the JAR file.
  7. Select or clear the Compress the contents of the JAR file checkbox.
  8. Select or clear the Overwrite existing files without warning checkbox. If you clear this checkbox, then you will be prompted to confirm the replacement of each file that will be overwritten.
  9. Note: The overwrite option is applied when writing the JAR file, the JAR description, and the manifest file.
  10. You have two options:

* Related Concepts

Java development tools (JDT)

* Related Tasks

Adding a JAR file to the build path
Attaching source to a JAR file
Defining the JAR file's manifest
Setting advanced options

* Related Reference

JAR file exporter
Package Explorer


Another set of tips from a former student, Michelle Arnone

The command to run jar is

"C:\Program Files\Java\jdk1.5.0_03\bin\jar.exe"  Note there is only one C:\ after the quote as well.  The quotes are necessary when there is a space a folder name.

*** But first make a copy of your files and back them up somewhere safe in case you accidentally overwrite while learning to use jar.exe! ***

Be sure via Windows Explorer or My Computer that the bin folder actually exists in the jdk1.5.0_03 folder. (Or whatever the name of the folder is that contains Java.) It may be slightly different if you have a different version installed, so change the path accordingly if necessary.

Bear in mind that if you are not an administrator on your machine you may not have access to write anything to the folder in Program Files.

So when you run the command given above, you have to run it from a directory where you have access to write, such as My Documents.  If you are running it from a directory other than the one where the .java files are located, you have to specify that path to the files in the command prompt.

For example, if I have a folder in My Documents called Java, and I want to put myJar.jar there, and I have and in a folder in My Documents called My Folder, the command would look like this:

C:\Documents and Settings\michelle\My Documents>"C:\Program Files\Java\jdk1.5.0_03\bin\jar.exe" cf myJar.jar "C:\Documents and Settings\michelle\My Documents\" "C:\Documents and Settings\michelle\My Documents\"

Other web pages that discuss how to create jar files.

To the CS 307 home page