To deploy your applet, first compile the source code and package it as a JAR file.
Applets can be launched in two ways.
- You can launch an applet by specifying the applet's launch properties directly in the applet tag. This old way of deploying applets imposes severe security restrictions on the applet.
- Alternatively, you can launch your applet by using Java Network Launch Protocol (JNLP). Applets launched by using JNLP have access to powerful JNLP APIs and extensions.
The Deployment Toolkit script contains useful JavaScript functions that can be used to deploy applets in a web page.
If you are unfamiliar with these deployment technologies, review the Deployment In-Depth lesson before proceeding further.
Here are some step-by-step instructions to package and deploy your applet. The Dynamic Tree Demo applet is used to illustrate applet deployment. You might want to set up build scripts to execute some of the following steps.
Note: If you don't see the applet running, make sure that you have at least the Java 2 Platform, Standard Edition (J2SE) 1.4.2 release on your client. If not, download and install the latest release of the Java SE Development Kit (JDK).
Note: If you don't see the example running, you might need to enable the JavaScript interpreter in your browser so that the Deployment Toolkit script can function properly.
- Compile your applet's Java code and make sure all class files and resources such as images are in a separate directory.
In the case of the DynamicTree Demo applet, the compiled classes are placed in the
build/classes/appletComponentArch
directory.- Create a JAR file containing your applet's class files and resources.
For example, the following command creates a JAR file with the class files in the
build/classes/appletComponentArch
directory.See the Packaging Programs in JAR Files lesson to learn more about creating and using JAR files.cd build/classes jar cvf DynamicTreeDemo.jar appletComponentArch- Create a JNLP file that describes how your applet should be launched.
Here is the JNLP file used to launch the Dynamic Tree Demo applet.
The source forfollows:
dynamictree-applet.jnlp
The topic, Structure of the JNLP File, describes JNLP file syntax and options.<?xml version="1.0" encoding="UTF-8"?> <jnlp spec="1.0+" codebase="" href=""> <information> <title>Dynamic Tree Demo</title> <vendor>Dynamic Team</vendor> </information> <resources> <!-- Application Resources --> <j2se version="1.6+" href="http://java.sun.com/products/autodl/j2se" /> <jar href="DynamicTreeDemo.jar" main="true" /> </resources> <applet-desc name="Dynamic Tree Demo Applet" main-class="components.DynamicTreeApplet" width="300" height="300"> </applet-desc> <update check="background"/> </jnlp>- Create the HTML page that will display the applet. Invoke Deployment Toolkit functions to deploy the applet.
In our example, the Dynamic Tree Demo applet is deployed in
.
AppletPage.html
<body> .... <script src="http://www.java.com/js/deployJava.js"></script> <script> var attributes = { code:'components.DynamicTreeApplet', width:300, height:300} ; var parameters = {jnlp_href: 'dynamictree-applet.jnlp'} ; deployJava.runApplet(attributes, parameters, '1.6'); </script> .... </body>- Place the applet's JAR file, JNLP file and HTML page in the appropriate folder(s).
For this example, place
DynamicTreeDemo.jar
,dynamictree-applet.jnlp
, andAppletPage.html
in the same directory on the local machine or a web server. A web server is not required for testing this applet.- Open the applet's HTML page in a browser to view the applet. Check the Java Console log for error and debugging messages.
Download source code for the Dynamic Tree Demo Applet example to experiment further.