|
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:
This section will cover the first two situations. A separate trail in the tutorial on the extension mechanism covers the use of JAR files as extensions. Applets Packaged in JAR FilesTo 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>
</applet>
If the TicTacToe demo were packaged in a JAR file named
<applet code=TicTacToe.class
archive="TicTacToe.jar"
width=120 height=120>
</applet>
The ARCHIVE parameter specifies the relative path to the JAR file that
contains
<applet code=TicTacToe.class
archive="applets/TicTacToe.jar"
width=120 height=120>
</applet>
Applications Packaged in JAR Files - 1.1 platform
You can run applications that are bundled as JAR files by using the
JDK 1.1 jre -cp app.jar MainClass
In version 1.1 of the JDK software, the JAR Files as Applications - 1.2 platform onlyIn version 1.2 of the JDK software, you can run JAR-packaged applications with the Java interpreter. The basic command is: java -jar jar-file
The Note: The -jar option is not available for interpreters prior to version 1.2 of the Java Development Kit. Before this command will work, however, the runtime environment needs to know which class within the JAR file is the application's entry point.
To indicate which class is the application's entry point,
you must add a Main-Class: classname
The header's value,
To create a JAR file having a manifest with the appropriate
Main-Class: HelloWorld
Assuming your text file was in a file called jar cmf mainClass app.jar HelloWorld.class With your JAR file prepared in this way, you can run the HelloWorld application from the command line: java -jar app.jar | |||||
|
| ||||||||||||