The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
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.以下是一些打包和部署小程序的分步说明。动态树演示小程序用于说明小程序的部署。您可能需要设置构建脚本来执行以下一些步骤。
In the case of the DynamicTree Demo applet, the compiled classes are placed in the 在DynamicTree Demo小程序的情况下,编译后的类被放置在build/classes/appletComponentArch
directory.build/classes/appletComponentArch
目录中。
For the DynamicTree Demo applet, create a file named 对于DynamicTree Demo小程序,在mymanifest.txt
in the build/classes
directory, and add the Permissions
, Codebase
, and Application-Name
attributes. build/classes
目录中创建一个名为mymanifest.txt
的文件,并添加Permissions
、Codebase
和Application-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.请参阅使用清单属性增强安全性课程,以了解有关可用清单属性的更多信息。
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 例如,以下命令创建一个JAR文件,其中类文件位于build/classes/appletComponentArch
directory and the manifest file in the build/classes
directory.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文件的更多信息。
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文件。
Here is the JNLP file used to launch the Dynamic Tree Demo applet.这是用于启动动态树演示小程序的JNLP文件。
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文件语法和选项。
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>
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.jar
、dynamictree_applet.jnlp
和AppletPage.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控制面板的“安全”选项卡管理。
Download source code for the Dynamic Tree Demo Applet example to experiment further.下载动态树演示Applet示例的源代码以进行进一步的实验。