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中文網其他相關文章!