Java 9 模块是作为包集合引入的新实体,以提供某种特性或功能。 Java 9模块是Java开发工具包重新排列和分类为模块后的第一个版本。它也称为 Java 9 平台模块系统 (JPMS)。 在 java 9 中,您可以通过仅包含所需的模块来减少运行时大小。例如,对于不支持 GUI 的设备,您可以创建不包含 GUI 模块的运行时。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
代码:
module eg.com.module1 { }
代码:
module eg.com.module1 { exports eg.com.module1.service; }
代码:
{ requires eg.com.module1; }
代码:
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 }
src\com.educba\com\educba
mkdir –r src\com.educba\com\educba
module com.educba { }
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.
Code:
>javap mods/com.educba/module-info.class
Output:
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.
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 代码重组,使其与分布式架构的新编码技术兼容。它通过选择包含所需的模块来帮助减少可执行文件的大小,从而提高性能。强大的封装性提高了安全性,同时减少了潜在黑客可用的类。模块化提供了依赖性声明和确定的透明度。
答案:你可以在Java 9中编译你的遗留应用程序。在编译时,你会遇到那些不属于Java 9模块化结构的代码部分的错误。这里你需要花费一些时间是时候包含所需的模块了。您可以使用 Jshell 测试新代码以获得所需的输出。
答案: java –list-modules 可用于列出模块。您需要参考文档来了解它们的功能。
答案:不。这取决于您对命令行界面的熟悉程度。 IDE 在后端完成许多用户不知道的工作。 CLI 是识别问题并解决问题的最佳选择。
答案:您需要在模块的模块定义中添加以下内容才能访问公共包,info.java和required_package_name。
以上是Java 9 模块的详细内容。更多信息请关注PHP中文网其他相关文章!