搜索

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
JVM性能与其他语言JVM性能与其他语言May 14, 2025 am 12:16 AM

JVM'SperformanceIsCompetitiveWithOtherRuntimes,operingabalanceOfspeed,安全性和生产性。1)JVMUSESJITCOMPILATIONFORDYNAMICOPTIMIZAIZATIONS.2)c提供NativePernativePerformanceButlanceButlactsjvm'ssafetyFeatures.3)

Java平台独立性:使用示例Java平台独立性:使用示例May 14, 2025 am 12:14 AM

JavaachievesPlatFormIndependencEthroughTheJavavIrtualMachine(JVM),允许CodeTorunonAnyPlatFormWithAjvm.1)codeisscompiledIntobytecode,notmachine-specificodificcode.2)bytecodeisisteredbytheybytheybytheybythejvm,enablingcross-platerssectectectectectross-eenablingcrossectectectectectection.2)

JVM架构:深入研究Java虚拟机JVM架构:深入研究Java虚拟机May 14, 2025 am 12:12 AM

TheJVMisanabstractcomputingmachinecrucialforrunningJavaprogramsduetoitsplatform-independentarchitecture.Itincludes:1)ClassLoaderforloadingclasses,2)RuntimeDataAreafordatastorage,3)ExecutionEnginewithInterpreter,JITCompiler,andGarbageCollectorforbytec

JVM:JVM与操作系统有关吗?JVM:JVM与操作系统有关吗?May 14, 2025 am 12:11 AM

JVMhasacloserelationshipwiththeOSasittranslatesJavabytecodeintomachine-specificinstructions,managesmemory,andhandlesgarbagecollection.ThisrelationshipallowsJavatorunonvariousOSenvironments,butitalsopresentschallengeslikedifferentJVMbehaviorsandOS-spe

Java:写一次,在任何地方跑步(WORA) - 深入了解平台独立性Java:写一次,在任何地方跑步(WORA) - 深入了解平台独立性May 14, 2025 am 12:05 AM

Java实现“一次编写,到处运行”通过编译成字节码并在Java虚拟机(JVM)上运行。1)编写Java代码并编译成字节码。2)字节码在任何安装了JVM的平台上运行。3)使用Java原生接口(JNI)处理平台特定功能。尽管存在挑战,如JVM一致性和平台特定库的使用,但WORA大大提高了开发效率和部署灵活性。

Java平台独立性:与不同的操作系统的兼容性Java平台独立性:与不同的操作系统的兼容性May 13, 2025 am 12:11 AM

JavaachievesPlatFormIndependencethroughTheJavavIrtualMachine(JVM),允许Codetorunondifferentoperatingsystemsswithoutmodification.thejvmcompilesjavacodeintoplatform-interploplatform-interpectentbybyteentbytybyteentbybytecode,whatittheninternterninterpretsandectectececutesoneonthepecificos,atrafficteyos,Afferctinginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginging

什么功能使Java仍然强大什么功能使Java仍然强大May 13, 2025 am 12:05 AM

JavaispoperfulduetoitsplatFormitiondence,对象与偏见,RichstandardLibrary,PerformanceCapabilities和StrongsecurityFeatures.1)Platform-dimplighandependectionceallowsenceallowsenceallowsenceallowsencationSapplicationStornanyDevicesupportingJava.2)

顶级Java功能:开发人员的综合指南顶级Java功能:开发人员的综合指南May 13, 2025 am 12:04 AM

Java的顶级功能包括:1)面向对象编程,支持多态性,提升代码的灵活性和可维护性;2)异常处理机制,通过try-catch-finally块提高代码的鲁棒性;3)垃圾回收,简化内存管理;4)泛型,增强类型安全性;5)ambda表达式和函数式编程,使代码更简洁和表达性强;6)丰富的标准库,提供优化过的数据结构和算法。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器