User Tools

Site Tools


Using JAR Files: The Basics

Extracting the Contents a JAR File

The basic command to use for extracting the contents of a JAR file is:

jar xf jar-file [archived-file(s)]

Creating a JAR File

The basic format of the command for creating a JAR file is:

jar cf jar-file input-file(s)

Viewing the Contents of a JAR File

The basic format of the command for viewing the contents of a JAR file is:

jar tf jar-file

Running JAR-Packaged Software

Now that you've learned how to create JAR files, how do you actually run the code that you've packaged? Consider these three scenarios:

  • Your JAR file contains an applet that is to be run inside a browser.
  • Your JAR file contains an application that is to be invoked from the command line.

This section will cover the first two situations.

TO sign a jar file you first need to generate a key:

keytool -genkey -keystore artemis -alias signLegal

Sign jar files:

jarsigner -keystore artemis -signedjar sartemis.jar artemis.jar signLegal 



Applets Packaged in JAR Files

To invoke any applet from an HTML file for running inside a browser, you need to use the APPLET tag. (See the Writing Applets trail for information on applets.) If the applet is bundled as a JAR file, the only thing you need to do differently is to use the ARCHIVE parameter to specify the relative path to the JAR file.

As an example, let's use (again!) the TicTacToe demo applet that ships with the Java Development Kit. The APPLET tag in the HTML file that calls the demo looks like this:

<applet code=TicTacToe.class 
        width=120 height=120>

If the TicTacToe demo were packaged in a JAR file named TicTacToe.jar, you could modify the APPLET tag with the simple addition of an ARCHIVE parameter:

<applet code=TicTacToe.class 
        width=120 height=120>

The ARCHIVE parameter specifies the relative path to the JAR file that contains TicTacToe.class. This example assumes that the JAR file and the HTML file are in the same directory. If they're not, you would need to include the JAR file's relative path in the ARCHIVE parameter's value. For example, if the JAR file was one directory below the HTML file in a directory called applets, the APPLET tag would look like this:

<applet code=TicTacToe.class 
        width=120 height=120>
java_jar_files.txt · Last modified: 2010/05/22 14:19 (external edit)