Using JAR Files: The Basics |
Now that you've learned how to create JAR files, how do you actually run the code that you've packaged? There are three scenarios to consider:
- 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.
- Your JAR file contains code that you want to use as an extension.
JAR Files as Applets
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 JavaTM Development Kit. The APPLET tag in the HTML file that calls the demo looks like this (ignoring the ALT tag for clarity):
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> </applet>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 specify the JAR file's path.<applet code=TicTacToe.class archive="TicTacToe.jar" width=120 height=120> </applet>
JAR Files as Applications - 1.1 platform
You can run applications that are bundled as JAR files by using the JDKTM 1.1 jre tool:jre -cp app.jar MainClassIn version 1.1 of the JDK software, the -cp option prepends the app.jar file to the system classpath. MainClass identifies the class within the JAR file that is the application's entry point. (Recall that in an application, one of the classes must have a method with the signature public static void main(String[] args) that serves as entry or starting point for the application.)
Using JAR Files: The Basics |