Home >Java >javaTutorial >Java 9 Modules

Java 9 Modules

WBOY
WBOYOriginal
2024-08-30 15:53:56640browse

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.

Java 9 Modules

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

 

Key Highlights

  • Java 9 module is the implementation of a high-level abstraction of packages.
  • It has all packages related to functionality packed into one module with a total of 95 modules. It provides greater platform integrity and strong encapsulation.
  • It enables the creation of a separate Java module as a modular JAR file for Java applications or Java API.

Java 9 Module System

  • In Java 9 modules, the functionally related packages are aggregated into reusable groups called modules.
  • The Java 9 module may contain resources (such as images and XML files). java.base module is an independent module that is necessary for every module.
  • The System Modules are standard, and their names start with java.
  • For example, JavaFX modules(names starting with JavaFX), JDK modules(names starting with jdk), Oracle modules(names starting with oracle).

Types of Java 9 Modules

1. Application modules

  • These modules are created by programmers.
  • The assembled path unofficial JAR contains the compiled module-info.a class file that contains the name and definition of the module.

2. Automatic Modules

  • When existing JAR files are added to the module, automatic modules are created.
  • The name of the JAR becomes the name of the module.

3. Unnamed Module

  • It’s a class or JAR loaded onto the classpath but not into the module path.
  • It’s a catch-all module to maintain backward compatibility with previously-written Java code.

Modular Descriptor

  • The module-info.java file has the module definition or metadata that describes the module.
  • On compilation of module-info.java, we get module-info.class that contains the module descriptor, which is stored in the module’s root folder.
  • Modular descriptor consists of one or more exports and requires a clause.
  • It is an independent module as it can export the packages to other modules and can use the packages of other modules.
  • The metadata includes three things, unique name, exports clause, and requires clause.
  • Unique name: Name given to module.

Code:

module eg.com.module1 
{

}
  • Exports Clause: The packages in the module can be exported as other modules can use them.

Code:

module eg.com.module1 
{
   exports eg.com.module1.service;
}
  • Requires Clause: It is used when you are in need of packages from other modules.

Code:

{
   requires eg.com.module1;
}

Modules of Java 9 Module System Application

  • To list out the modules of the JDK’s set from the bin folder of jdk. Run the following command in CLI java –list-modules
  • The keyword module is used to declare the module. The module body is enclosed in braces, as in the: module modulename { }
  • An exports module directive makes the module’s package accessible to code in all other modules.
  • An exports…to define a qualified report. The list of packages that can access the exported package can be precisely specified in a comma-separated list.

Code:

module com.educba.util 
{
exports com.educba.app;
exports com.educba.security to com.educba.client, com.educba.producer;
}
  • When an open module directive is used, all the packages in a given module become accessible at runtime and via reflection to all other modules. To allow runtime-only access to all packages in a module, you may open the entire module, as in:

Code;

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 Modules

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.

Conclusion

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.

FAQs

Q1. How do I migrate my legacy application into JAVA 9?

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.

 Q2. How do I know which modules are provided by the system?

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.

Q3. Is it necessary to install IDE to work in JAVA?

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.

 Q4. How to access a public package in my module?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Java Date PickerNext article:Java Date Picker