Home >Java >javaTutorial >Java 9 Modules
Java 9 Module is the new entity that is introduced as a collection of packages to provide a certain feature or functionality. Java 9 module is the first release of the java development kit after the rearrangement and categorization of packages into modules. It is also known as Java 9 Platform Module System (JPMS). In java 9, you can reduce the runtime size by including only the required modules. For example, for a device that does not support GUIs, you can create a runtime without including the GUI modules.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Code:
module eg.com.module1 { }
Code:
module eg.com.module1 { exports eg.com.module1.service; }
Code:
{ requires eg.com.module1; }
Code:
module com.educba.util { exports com.educba.app; exports com.educba.security to com.educba.client, com.educba.producer; }
Code;
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 is indeed the most sought restructuring of java code to make it compatible with the new coding techniques of distributed architecture. It has helped in reducing the size of executables with the choice of including the required modules, hence improving performance. The strong encapsulation has enabled higher security with fewer classes available to potential hackers. The modularity has provided transparency in dependency declaration and determination.
Answer: You can compile your legacy application in Java 9. On compilation, you will encounter errors for those parts of code that are not in the modular structure of Java 9. Here you need to spend some time to include the required modules. You can test your new code using Jshell for the required output.
Answer: The java –list-modules can be used to list out the modules. You need to refer to the documentation to gain knowledge about their functions.
Answer: No. It depends on how comfortable you are with the Command line interface. IDEs do many jobs at the backend that are not known to users. CLI is the best option to identify the problem and resolve it.
Answer: You need to add the following in the module definition in your module to access the public package, info.java, and required_package_name.
The above is the detailed content of Java 9 Modules. For more information, please follow other related articles on the PHP Chinese website!