Java 教程是为 JDK 8 编写的。本页中描述的示例和实践未利用在后续版本中引入的改进。
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.
For the DynamicTree Demo applet, create a file named mymanifest.txt
in the build/classes
directory, and add the Permissions
, Codebase
, and Application-Name
attributes. 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 Application-Name: Dynamic Tree Demo
Other manifest attributes are available to restrict an applet to using only trusted code, and to provide security for applets that need to make calls between privileged Java code and sandbox Java code, or have JavaScript code that calls the applet. See the Enhancing Security with Manifest Attributes lesson to learn more about the manifest attributes that are available.
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.
If you want to use a signed JNLP file for security, create the JNLP file as described in the next step and include it in the JAR file before the JAR file is signed. See Signed JNLP Files in the Java Platform, Standard Edition Deployment Guide for 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.7+" 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="https://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.7'); </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 preferred. To run from the local machine, you must add your application to the exception site list, which is managed from the Security tab of the Java Control Panel.
Download source codefor the Dynamic Tree Demo Applet example to experiment further.