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发行说明。
Installed extensions are JAR files in the lib/ext directory of the Java Runtime Environment (JRE™) software. 已安装的扩展名是Java运行时环境(JRE™)的lib/ext目录中的JAR文件软件。As its name implies, the JRE is the runtime portion of the Java Development Kit containing the platform's core API but without development tools such as compilers and debuggers. 顾名思义,JRE是Java开发工具包的运行时部分,包含平台的核心API,但没有编译器和调试器等开发工具。The JRE is available either by itself or as part of the Java Development Kit.JRE可以单独使用,也可以作为Java开发工具包的一部分使用。
The JRE is a strict subset of the JDK software. JRE是JDK软件的严格子集。A subset of the JDK software directory tree looks like this:JDK软件目录树的一个子集如下所示:
The JRE consists of those directories within the highlighted box in the diagram. JRE由图中突出显示的框中的目录组成。Whether your JRE is stand-alone or part of the JDK software, any JAR file in the lib/ext of the JRE directory is automatically treated by the runtime environment as an extension.无论您的JRE是独立的还是JDK软件的一部分,运行时环境都会自动将JRE目录的lib/ext中的任何JAR文件视为扩展。
Since installed extensions extend the platform's core API, use them judiciously. 由于已安装的扩展扩展扩展了平台的核心API,所以请明智地使用它们。They are rarely appropriate for interfaces used by a single, or small set of applications.它们很少适用于单个或小型应用程序使用的接口。
Furthermore, since the symbols defined by installed extensions will be visible in all Java processes, care should be taken to ensure that all visible symbols follow the appropriate "reverse domain name" and "class hierarchy" conventions. 此外,由于已安装的扩展定义的符号在所有Java进程中都是可见的,因此应注意确保所有可见符号遵循适当的“反向域名”和“类层次结构”约定。For example, com.mycompany.MyClass.例如,com.mycompany.MyClass。
As of Java 6, extension JAR files may also be placed in a location that is independent of any particular JRE, so that extensions can be shared by all JREs that are installed on a system. 从Java 6开始,扩展JAR文件也可以放置在独立于任何特定JRE的位置,这样系统上安装的所有JRE都可以共享扩展。Prior to Java 6, the value of java.ext.dirs referred to a single directory, but as of Java 6 it is a list of directories (like CLASSPATH) that specifies the locations in which extensions are searched for. 在Java 6之前,java.ext.dirs的值指的是一个目录,但从Java 6开始,它是一个目录列表(比如CLASSPATH),指定了搜索扩展的位置。The first element of the path is always the lib/ext directory of the JRE. The second element is a directory outside of the JRE. 路径的第一个元素始终是JRE的lib/ext目录。第二个元素是JRE之外的目录。This other location allows extension JAR files to be installed once and used by several JREs installed on that system. 另一个位置允许一次安装扩展JAR文件,并由该系统上安装的几个JRE使用。The location varies depending on the operating system:位置因操作系统而异:
Note that an installed extension placed in one of the above directories extends the platform of each of the JREs (Java 6 or later) on that system.请注意,安装在上述目录之一中的扩展扩展扩展了该系统上每个JRE(Java 6或更高版本)的平台。
Let's create a simple installed extension. 让我们创建一个简单的已安装扩展。Our extension consists of one class, RectangleArea, that computes the areas of rectangles:扩展由一个类RectangleArea组成,用于计算矩形的面积:
public final class RectangleArea { public static int area(java.awt.Rectangle r) { return r.width * r.height; } }
This class has a single method, area, that takes an instance of java.awt.Rectangle and returns the rectangle's area.这个类只有一个方法area,它以java.awt.Rectangle为实例并返回矩形的面积。
Suppose that you want to test RectangleArea with an application called 假设您要使用名为AreaApp
:AreaApp
的应用程序测试RectangleArea:
import java.awt.*; public class AreaApp { public static void main(String[] args) { int width = 10; int height = 5; Rectangle r = new Rectangle(width, height); System.out.println("The rectangle's area is " + RectangleArea.area(r)); } }
This application instantiates a 10 x 5 rectangle, and then prints out the rectangle's area using the RectangleArea.area method.此应用程序实例化一个10 x 5的矩形,然后使用RectangleAreaarea方法打印出矩形的面积。
Let's first review how you would run the 让我们首先回顾一下在不使用扩展机制的情况下如何运行AreaApp
application without using the extension mechanism. AreaApp
应用程序。We'll assume that the RectangleArea class is bundled in a JAR file named area.jar.我们假设RectangleArea类捆绑在一个名为area.jar的JAR文件中。
The RectangleArea class is not part of the Java platform, of course, so you would need to place the area.jar file on the class path in order to run 当然,RectangleArea类不是Java平台的一部分,因此您需要将area.jar文件放在类路径上,以便运行AreaApp
without getting a runtime exception. AreaApp
而不会出现运行时异常。If area.jar was in the directory /home/user, for example, you could use this command:例如,如果area.jar位于目录/home/user中,则可以使用以下命令:
java -classpath .:/home/user/area.jar AreaApp
The class path specified in this command contains both the current directory, containing AreaApp.class, and the path to the JAR file containing the RectangleArea package. 此命令中指定的类路径包含当前目录(包含AreaApp.class)和指向包含RectangleArea包的JAR文件的路径。You would get the desired output by running this command:通过运行以下命令,可以获得所需的输出:
The rectangle's area is 50
Now let's look at how you would run 现在,让我们看看如何使用RectangleArea类作为扩展来运行AreaApp
by using the RectangleArea class as an extension.AreaApp
。
To make the RectangleArea class into an extension, you place the file area.jar in the lib/ext directory of the JRE. 要将RectangleArea类变成一个扩展,可以将文件area.jar放在JRE的lib/ext目录中。Doing so automatically gives the RectangleArea the status of being an installed extension.这样做会自动使RectangleArea处于已安装扩展的状态。
With area.jar installed as an extension, you can run 安装area.jar作为扩展后,无需指定类路径即可运行AreaApp
without needing to specify the class path:AreaApp
:
java AreaApp
Because you're using area.jar as an installed extension, the runtime environment will be able to find and to load the 因为您正在使用area.jar作为已安装的扩展,所以运行时环境将能够找到并加载RectangleArea
class even though you haven't specified it on the class path. RectangleArea
类,即使您没有在类路径上指定它。Similarly, any applet or application being run by any user on your system would be able to find and use the RectangleArea class.类似地,系统上任何用户运行的任何小程序或应用程序都能够找到并使用RectangleArea类。
If there are multiple JREs (Java 6 or later) installed on a system and want the RectangleArea class to be available as an extension to all of them, instead of installing it in the lib/ext directory of a particular JRE, install it in the system-wide location. 如果系统上安装了多个JRE(Java 6或更高版本),并且希望RectangleArea类可以作为所有JRE的扩展,请将其安装在系统范围内的位置,而不是安装在特定JRE的lib/ext目录中。For example, on system running Linux, install area.jar in the directory /usr/java/packages/lib/ext. 例如,在运行Linux的系统上,在/usr/java/packages/lib/ext目录下安装area.jar。Then AreaApp can run using different JREs that are installed on that system, for example if different browsers are configured to use different JREs.然后,AreaApp可以使用安装在该系统上的不同JRE运行,例如,如果不同的浏览器配置为使用不同的JRE。