首页  >  文章  >  Java  >  Java 9 模块

Java 9 模块

WBOY
WBOY原创
2024-08-30 15:53:56586浏览

Java 9 模块是作为包集合引入的新实体,以提供某种特性或功能。 Java 9模块是Java开发工具包重新排列和分类为模块后的第一个版本。它也称为 Java 9 平台模块系统 (JPMS)。 在 java 9 中,您可以通过仅包含所需的模块来减少运行时大小。例如,对于不支持 GUI 的设备,您可以创建不包含 GUI 模块的运行时。

Java 9 模块

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

 

主要亮点

  • Java 9 模块是包的高级抽象的实现。
  • 它将所有与功能相关的包打包到一个模块中,总共有 95 个模块。它提供了更好的平台完整性和强大的封装性。
  • 它允许创建单独的 Java 模块作为 Java 应用程序或 Java API 的模块化 JAR 文件。

Java 9 模块系统

  • 在 Java 9 模块中,功能相关的包被聚合到可重用的组中,称为模块。
  • Java 9 模块可能包含资源(例如图像和 XML 文件)。 java.base模块是一个独立的模块,是每个模块所必需的。
  • 系统模块是标准的,其名称以java开头。
  • 例如,JavaFX 模块(名称以 JavaFX 开头)、JDK 模块(名称以 jdk 开头)、Oracle 模块(名称以 oracle 开头)。

Java 9 模块的类型

1.应用模块

  • 这些模块是由程序员创建的。
  • 组装路径非官方JAR包含编译后的module-info.a类文件,其中包含模块的名称和定义。

2.自动模块

  • 将现有 JAR 文件添加到模块时,会创建自动模块。
  • JAR 的名称成为模块的名称。

3.未命名模块

  • 它是加载到类路径但未加载到模块路径中的类或 JAR。
  • 它是一个包罗万象的模块,用于保持与以前编写的 Java 代码的向后兼容性。

模块化描述符

  • module-info.java 文件包含模块定义或描述模块的元数据。
  • 编译 module-info.java 时,我们得到包含模块描述符的 module-info.class,该描述符存储在模块的根文件夹中。
  • 模块化描述符由一个或多个导出组成,并且需要一个子句。
  • 它是一个独立的模块,可以将包导出到其他模块,并且可以使用其他模块的包。
  • 元数据包括三件事,唯一名称、exports 子句和requires 子句。
  • 唯一名称:为模块指定的名称。

代码:

module eg.com.module1 
{

}
  • 导出子句:模块中的包可以导出,以便其他模块可以使用它们。

代码:

module eg.com.module1 
{
   exports eg.com.module1.service;
}
  • Requires 子句:当你需要其他模块的包时使用。

代码:

{
   requires eg.com.module1;
}

Java 9 模块系统应用的模块

  • 从jdk的bin文件夹中列出JDK集合的模块。在 CLI java –list-modules
  • 中运行以下命令
  • 关键字 module 用于声明模块。模块主体用大括号括起来,如: module modulename { }
  • exports 模块指令使所有其他模块中的代码都可以访问该模块的包。
  • 导出...来定义合格报告。 可以在逗号分隔的列表中精确指定可以访问导出包的包列表。

代码:

module com.educba.util 
{
exports com.educba.app;
exports com.educba.security to com.educba.client, com.educba.producer;
}
  • 当使用开放模块指令时,给定模块中的所有包都可以在运行时通过反射到所有其他模块来访问。要允许仅运行时访问模块中的所有包,您可以打开整个模块,如下所示:

代码;

open module <em>modulename</em>

{
// module directives
}
  • To allow runtime-only access to packages in a module. An open module directive of the form opens package- can be used. The code in other modules can access the public types of a package and its nested public and protected types at runtime only. Reflection can be used to access all the types and the types’ members in the specified package.
  • The code in the listed modules can access the public types of a package and its nested public and protected types at runtime only. Reflection to code in the specified modules can be used to access all of the types and types’ members in the specified package.

How to Create a New Java 9 Module?

  • Create a directory structure
src\com.educba\com\educba
  • In the command prompt
mkdir –r src\com.educba\com\educba
  • Create a new module in a text editor and save it as a java file module-info.java.
module com.educba
{ }
  • Set the environment variable JAVA_HOME to the path where Java is installed.

For example, JAVA_HOME C:\Program Files\Java\jdk-9\bin

Add %JAVA_HOME% to PATH in control Panel->system->advanced system setting->environment variables.

  • To see the compiled module descriptor in the CLI.

Code:

>javap mods/com.educba/module-info.class

Output:

Java 9 模块

It shows requires java.base in the modules definition, though we had created an empty module. It is because java.base is the basic and independent module. All other modules depend on it.

Comparison Table of JDK 8 and JDK 9

The most obvious difference between JDK 8 and JDK 9.

Feature Java 8 Java 9
Top Level component Package Module
New features launched ●       Lambda Expressions

●       Stream API

●       Date API

●       Java Module System (Jigsaw Project)

●       Java REPL

●       Milling Project Coin

Performance Compromised performance due to big size of jdk Improved performance
Testing and maintaining applications Difficult with large size of JRE Easy with REPL and reduced size depending on modules included. JShell or REPL: Read and evaluate Print Loop for easy execution and testing of   Java Constructs like class, interface, enum, object, and statements easily.
Security It is compromised because of no encapsulation. Internal APIs can be accessed easily by users and potential hackers. Reflection could be used to learn about private members too. String security as Reflection does not provide access to private members. Only those exposed by export keywords are available through reflection.
Packaging format JAR JMOD can include native code and configuration files.

结论

Java 9 确实是最受欢迎的 Java 代码重组,使其与分布式架构的新编码技术兼容。它通过选择包含所需的模块来帮助减少可执行文件的大小,从而提高性能。强大的封装性提高了安全性,同时减少了潜在黑客可用的类。模块化提供了依赖性声明和确定的透明度。

常见问题解答

Q1。如何将旧应用程序迁移到 JAVA 9?

答案:你可以在Java 9中编译你的遗留应用程序。在编译时,你会遇到那些不属于Java 9模块化结构的代码部分的错误。这里你需要花费一些时间是时候包含所需的模块了。您可以使用 Jshell 测试新代码以获得所需的输出。

 Q2。我如何知道系统提供了哪些模块?

答案: java –list-modules 可用于列出模块。您需要参考文档来了解它们的功能。

Q3。 JAVA需要安装IDE吗?

答案:不。这取决于您对命令行界面的熟悉程度。 IDE 在后端完成许多用户不知道的工作。 CLI 是识别问题并解决问题的最佳选择。

 Q4。如何访问我的模块中的公共包?

答案:您需要在模块的模块定义中添加以下内容才能访问公共包,info.javarequired_pa​​ckage_name

以上是Java 9 模块的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn