To deploy your Java applet, first compile the source code, package it as a JAR file, and sign the JAR file.
Java applets can be launched in two ways.
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.
In the case of the DynamicTree Demo applet, the compiled classes are placed in the build/classes/appletComponentArch
directory.
Permissions
and Codebase
attributes for the JAR manifest.
For the DynamicTree Demo applet, create a file named mymanifest.txt
in the build/classes
directory. The applet does not require access to the user's system resources, so use sandbox
for the permissions. Use the domain from which you will load the sample for the code base, for example, myserver.com. Add the following attributes to the mymanifest.txt
file.
Permissions: sandbox Codebase: myserver.com
See the Enhancing the Security of the JAR File lesson to learn more about these manifest attributes.
mymanifest.txt
file that you created in the previous step.
For example, the following command creates a JAR file with the class files in the build/classes/appletComponentArch
directory and the manifest file in the build/classes
directory.
% cd build/classes % jar cvfm DynamicTreeDemo.jar mymanifest.txt appletComponentArch
See the Packaging Programs in JAR Files lesson to learn more about creating and using JAR files.
See the Signing JAR Files lesson for more information.
Here is the JNLP file used to launch the Dynamic Tree Demo applet.
The source fordynamictree_applet.jnlp
follows:
<?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>
Note that the security element for requesting additional permissions is not present in the JNLP file, therefore the applet runs only in the security sandbox.
The topic, Structure of the JNLP File, describes JNLP file syntax and options.
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>
For this example, place DynamicTreeDemo.jar
, dynamictree_applet.jnlp
, and AppletPage.html
in the same directory on the local machine or a web server. A web server is not required for testing this applet.
Download source code for the Dynamic Tree Demo Applet example to experiment further.