Documentation

The Java™ Tutorials
Hide TOC
Deploying a Java Web Start Application部署Java Web Start应用程序
Trail: Deployment
Lesson: Java Web Start

Deploying a Java Web Start Application部署Java Web Start应用程序

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

Java Web Start applications are launched by using the Java Network Launch Protocol (JNLP). Java Web Start应用程序是通过使用Java网络启动协议(JNLP)启动的。Hence, you must create a JNLP file to deploy your application.因此,必须创建一个JNLP文件来部署应用程序。

The Deployment Toolkit script contains useful JavaScript functions that can be used to deploy Java Web Start applications on a web page.部署工具包脚本包含有用的JavaScript函数,可用于在网页上部署Java Web Start应用程序。

If you are unfamiliar with these deployment technologies, review the Deployment In-Depth lesson before proceeding.如果您不熟悉这些部署技术,请在继续之前查看深入部署课程。

Here are some step-by-step instructions to package and deploy your application. 下面是一些打包和部署应用程序的分步说明。The Dynamic Tree Demo application is used to illustrate the deployment of Java Web Start applications. 动态树演示应用程序用于演示Java Web Start应用程序的部署。You might want to set up build scripts to execute some of the following steps.您可能需要设置构建脚本来执行以下一些步骤。

Click the following Launch button to launch the Dynamic Tree Demo application.单击下面的启动按钮启动动态树演示应用程序。


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 application's Java code and make sure that all class files and resources such as images are in a separate directory. 编译应用程序的Java代码,并确保所有类文件和资源(如图像)都位于单独的目录中。

    In the Dynamic Tree Demo application, the compiled classes are placed in the build/classes/webstartComponentArch directory.在动态树演示应用程序中,编译的类被放置在build/classes/webstartComponentArch目录中。

  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演示小程序,在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. 使用将从中加载代码库示例的域,例如myserver.comAdd the following attributes to the mymanifest.txt file.将以下属性添加到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 application'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/webstartComponentArch directory and the manifest file in the build/classes directory.例如,下面的命令创建了一个JAR文件,其中类文件位于build/classes/webstartComponentArch目录,清单文件位于build/classes目录。

    % cd build/classes
    % jar cvfm  DynamicTreeDemo.jar  mymanifest.txt webstartComponentArch

    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 application should be launched. 创建一个JNLP文件,描述应该如何启动应用程序。

    Here is the JNLP file that is used to launch the Dynamic Tree Demo application. 下面是用于启动动态树演示应用程序的JNLP文件。Permissions are not requested for this application so it runs in the security sandbox. 此应用程序未请求权限,因此它在安全沙箱中运行。The source for dynamictree_webstart.jnlp follows:dynamictree_webstart.jnlp的来源如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <jnlp spec="1.0+" codebase=
    "https://docs.oracle.com/javase/tutorialJWS/samples/deployment/webstart_ComponentArch_DynamicTreeDemo" 
        href="dynamictree_webstart.jnlp"> <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> <application-desc
             name="Dynamic Tree Demo Application"
             main-class=
               "webstartComponentArch.DynamicTreeApplication"
             width="300"
             height="300"> </application-desc> <update check="background"/>
    </jnlp>
    Structure of the JNLP FileJNLP文件的结构 describes the JNLP file syntax and options. 描述JNLP文件语法和选项。

    Note: The codebase and href attributes are optional when deploying Java Web Start applications that will run on at least the Java SE 6 update 18 release or later. 在部署至少在Java SE 6 update 18或更高版本上运行的Java Web Start应用程序时,codebasehref属性是可选的。You must specify the codebase and href attributes when deploying Java Web Start applications that will run with previous releases of the Java Runtime Environment software. 部署Java Web Start应用程序时,必须指定codebasehref属性,这些应用程序将与Java运行时环境软件的早期版本一起运行。
  6. Create the HTML page from which your application will be launched. 创建将从中启动应用程序的HTML页面。Invoke Deployment Toolkit functions to deploy the Java Web Start application. 调用部署工具包函数来部署Java Web Start应用程序。

    In the example, the Dynamic Tree Demo application is deployed in JavaWebStartAppPage.html.在本例中,动态树演示应用程序部署在JavaWebStartAppPage.html中。

    <body> <!-- ... --> <script src=
          "https://www.java.com/js/deployJava.js"></script> <script> // using JavaScript to get location of JNLP
            // file relative to HTML page
            var dir = location.href.substring(0,
                location.href.lastIndexOf('/')+1);
            var url = dir + "dynamictree_webstart.jnlp";
            deployJava.createWebStartLaunchButton(url, '1.7.0');
        </script> <!-- ... -->
    </body>

    If you are not sure whether your end users will have the JavaScript interpreter enabled in their browsers, you can deploy the Java Web Start application directly by creating a link to the JNLP file as follows:如果您不确定最终用户是否会在浏览器中启用JavaScript解释器,可以通过创建指向JNLP文件的链接直接部署Java Web Start应用程序,如下所示:

    <a href="/absolute path to JNLP file/dynamictree_webstart.jnlp">Launch Notepad Application</a>

    If you deploy the Java Web Start application with a direct link, you cannot take advantage of the additional checks that the Deployment Toolkit functions provide. 如果使用直接链接部署Java Web Start应用程序,则无法利用部署工具包功能提供的附加检查。See Deploying a Java Web Start Application in the Deployment In-Depth lesson for details.有关详细信息,请参阅深度部署课程中的部署Java Web Start应用程序

  7. Place the application's JAR file, JNLP file, and HTML page in the appropriate folders. 将应用程序的JAR文件、JNLP文件和HTML页面放在适当的文件夹中。

    For this example, place DynamicTreeDemo.jar, dynamictree_webstart.jnlp, and JavaWebStartAppPage.html in the same directory on the local machine or a web server. 对于本例,请将DynamicTreeDemo.jardynamictree_webstart.jnlpJavaWebStartAppPage.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.最好是网络服务器。要从本地计算机运行,必须将应用程序添加到异常站点列表中,该列表由Java控制面板的“安全”选项卡管理。

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

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


Previous page: Retrieving Resources
Next page: Setting Up a Web Server