首頁  >  文章  >  Java  >  Java 9 模組

Java 9 模組

WBOY
WBOY原創
2024-08-30 15:53:56589瀏覽

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