Java 9 模組是作為包集合引入的新實體,以提供某種特性或功能。 Java 9模組是Java開發工具包重新排列和分類為模組後的第一個版本。它也稱為 Java 9 平台模組系統 (JPMS)。 在 java 9 中,您可以透過只包含所需的模組來減少執行時間大小。例如,對於不支援 GUI 的設備,您可以建立不包含 GUI 模組的執行時間。
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
主要亮點
- 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:
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.java和required_package_name。
以上是Java 9 模組的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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

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

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

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

Java實現“一次編寫,到處運行”通過編譯成字節碼並在Java虛擬機(JVM)上運行。 1)編寫Java代碼並編譯成字節碼。 2)字節碼在任何安裝了JVM的平台上運行。 3)使用Java原生接口(JNI)處理平台特定功能。儘管存在挑戰,如JVM一致性和平台特定庫的使用,但WORA大大提高了開發效率和部署靈活性。

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

JavaispoperfulduetoitsplatFormitiondence,對象與偏見,RichstandardLibrary,PerformanceCapabilities和StrongsecurityFeatures.1)Platform-dimplighandependectionceallowsenceallowsenceallowsenceallowsencationSapplicationStornanyDevicesupportingJava.2)

Java的頂級功能包括:1)面向對象編程,支持多態性,提升代碼的靈活性和可維護性;2)異常處理機制,通過try-catch-finally塊提高代碼的魯棒性;3)垃圾回收,簡化內存管理;4)泛型,增強類型安全性;5)ambda表達式和函數式編程,使代碼更簡潔和表達性強;6)豐富的標準庫,提供優化過的數據結構和算法。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver Mac版
視覺化網頁開發工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

Dreamweaver CS6
視覺化網頁開發工具