UMBC CMSC 331 Principles of Programming Languages

 

Java Jar files

The Java jar (Java ARchive) file mechanism is a good way of packaging up Java classes for storage and execution. A Java jar (Java ARchive) file is like a zip file or a tar file: it holds any number of compressed files. In addition to extracing and uncompressing the files for examination, you can also load a jar file into the jvm and even execute a jar file, if you've configured it correctly.

Jar files can be created from the command line via the jar command or via most Java IDEs, including BlueJ.

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.

Creating jar files with BlueJ

  1. Open your project in BlueJ, and make sure it runs correctly.
  2. From the Project menu, choose Export...
  3. In the dialog window that appears:
    1. Choose Store as jar file
    2. For the main class:, choose the class in your program that has the main method you want the program to start from (remember, any class may have a main method, so BlueJ doesn't know which one you want).
    3. Make sure include source is checked (necessary for grading purposes).
    4. Click Continue.
  4. In the next dialog window, choose a directory to put your jar file in, type a name for it where asks for a File Name: (the name should end in .jar), and click Create.

Creating jar files manually

  1. If possible, collect all your .java files and .class files (and anything else you want to include) together in a single directory.
  2. Using a text editor, create a file (say, myManifest) containing the following lines:

          Manifest-Version: 1.0
          Main-Class: MyMainClass

    where MyMainClass is the name of the class containing the main method you want to use.
  3. From the command line, execute the command:

         jar cvfm myResult.jar myManifest *.java *.class

    where myResult.jar is the jar file you are trying to create, myManifest is the file you created in step 2, and everything else is the files you want to include.

Getting at the contents of a jar file

Given a jar file you can view its directory and extract files using the jar command. To see the files stored in binks.jar, use the command

jar tf binks.jar

and to extract the file Foo.java use the command

jar xf binks.jar Foo.java

Executing an executable jar file

An executable jar file can be invoked in two ways: from the command line or from a GUI by double cliking it.

At the DOS or UNIX command prompt, type java -jar myResult.jar. This should work; if it doesn't, there are two main possibilities:

  • 'java' is not recognized as an internal or external command, operable program or batch file means that you don't have your system configured properly to run Java.
  • Exception in thread "main" java.lang.NoClassDefFoundError: MyMainClass means that the jar file was not constructed properly. The most likely error is having something in the wrong directory when you created the jar.

You can invoke an executable jar file from a GUI by double-clicking on the jar file icon. This will work if your system has been configured to know about jar files.

For more information

For more information, see the Sun Java tutorial's section on jar files and the reference pages for the Unix jar tool or Windows jar tool.