Documentation

The Java™ Tutorials
Hide TOC
Deploying an Applet部署小程序
Trail: Deployment
Lesson: Java Applets
Section: Getting Started With Applets小程序入门

Deploying an Applet部署小程序

To deploy your Java applet, first compile the source code, package it as a JAR file, and sign the JAR file.要部署Java小程序,首先编译源代码,将其打包为JAR文件,然后对JAR文件进行签名。

Java applets can be launched in two ways.Java小程序可以通过两种方式启动。

The Deployment Toolkit script contains useful JavaScript functions that can be used to deploy applets in a web page.部署工具包脚本包含有用的JavaScript函数,可用于在网页中部署小程序。

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 example running, you might need to enable the JavaScript interpreter in your browser so that the Deployment Toolkit script can function properly.如果您没有看到该示例正在运行,您可能需要在浏览器中启用JavaScript解释器,以便部署工具包脚本能够正常运行。
  1. Compile your applet's Java code and make sure all class files and resources such as images are in a separate directory. 编译小程序的Java代码,并确保所有类文件和资源(如图像)都在一个单独的目录中。

    In the case of the DynamicTree Demo applet, the compiled classes are placed in the build/classes/appletComponentArch directory.在DynamicTree Demo小程序的情况下,编译后的类被放置在build/classes/appletComponentArch目录中。

  2. Create a text file that contains any JAR file manifest attributes that your applet needs. 创建一个文本文件,其中包含小程序所需的任何JAR文件清单属性。

    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. 对于DynamicTree Demo小程序,在build/classes目录中创建一个名为mymanifest.txt的文件,并添加PermissionsCodebaseApplication-Name属性。The applet does not require access to the user's system resources, so use sandbox for the permissions.小程序不需要访问用户的系统资源,因此使用sandbox获取权限。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.使用将从中加载代码库示例的域,例如myserver.com。将以下属性添加到mymanifest.txt文件中。

    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. 其他清单属性可用于限制小程序仅使用受信任的代码,并为需要在特权Java代码和沙盒Java代码之间进行调用或具有调用小程序的JavaScript代码的小程序提供安全性。See the Enhancing Security with Manifest Attributes lesson to learn more about the manifest attributes that are available.请参阅使用清单属性增强安全性课程,以了解有关可用清单属性的更多信息。

  3. Create a JAR file containing your applet's class files and resources. 创建一个JAR文件,其中包含小程序的类文件和资源。Include the manifest attributes in the mymanifest.txt file that you created in the previous step. 在上一步中创建的mymanifest.txt文件中包括清单属性。

    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.例如,以下命令创建一个JAR文件,其中类文件位于build/classes/appletComponentArch目录中,清单文件位于build/calasses目录中。

    % 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.请参阅JAR文件中的打包程序课程,以了解有关创建和使用JAR文件的更多信息。

  4. Sign the JAR file for your applet and time stamp the signature. 对小程序的JAR文件进行签名,并在签名上加上时间戳。Use a valid, current code signing certificate issued by a trusted certificate authority to provide your users with assurance that it is safe to run the applet. 使用由受信任的证书颁发机构颁发的有效的当前代码签名证书,为用户提供安全运行小程序的保证。

    See the Signing JAR Files lesson for more information.有关更多信息,请参阅签名JAR文件课程。

    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. 如果您想使用已签名的JNLP文件来实现安全性,请按照下一步中的描述创建JNLP文件,并在对JAR文件进行签名之前将其包含在JAR文件中。See Signed JNLP Files in the Java Platform, Standard Edition Deployment Guide for information.有关信息,请参阅Java平台标准版部署指南中的签名JNLP文件。

  5. Create a JNLP file that describes how your applet should be launched. 创建一个JNLP文件,描述如何启动小程序。

    Here is the JNLP file used to launch the Dynamic Tree Demo applet.这是用于启动动态树演示小程序的JNLP文件。

    The source for dynamictree_applet.jnlp follows:dynamictree_applet.jnlp的来源如下:
    <?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.请注意,用于请求额外权限的安全元素不存在于JNLP文件中,因此小程序仅在安全沙盒中运行。

    The topic, Structure of the JNLP File, describes JNLP file syntax and options.主题JNLP文件的结构描述了JNLP文件语法和选项。

  6. Create the HTML page that will display the applet. Invoke Deployment Toolkit functions to deploy the applet. 创建将显示小程序的HTML页面。调用部署工具包函数来部署小程序。

    In our example, the Dynamic Tree Demo applet is deployed in AppletPage.html.在示例中,动态树演示小程序部署在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>
  7. Place the applet's JAR file, JNLP file and HTML page in the appropriate folder(s). 将小程序的JAR文件、JNLP文件和HTML页面放在适当的文件夹中。

    For this example, place DynamicTreeDemo.jar, dynamictree_applet.jnlp, and AppletPage.html in the same directory on the local machine or a web server. 对于本例,请将DynamicTreeDemo.jardynamictree_applet.jnlpAppletPage.html放在本地计算机或web服务器的同一目录中。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.首选web服务器。要在本地计算机上运行,必须将应用程序添加到异常站点列表中,该列表由Java控制面板的“安全”选项卡管理。

  8. Open the applet's HTML page in a browser to view the applet. Agree to run the applet when prompted. Check the Java Console log for error and debugging messages.在浏览器中打开小程序的HTML页面以查看小程序。同意在提示时运行小程序。检查Java控制台日志中的错误和调试消息。

Download source code for the Dynamic Tree Demo Applet example to experiment further.下载动态树演示Applet示例的源代码以进行进一步的实验。


Previous page: Developing an Applet
Next page: Deploying With the Applet Tag