Home > Java > javaTutorial > body text

Java 9 Modules

WBOY
Release: 2024-08-30 15:53:56
Original
584 people have browsed it

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 
{

}
Copy after login
  • 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;
}
Copy after login
  • Requires Clause: It is used when you are in need of packages from other modules.

Code:

{
   requires eg.com.module1;
}
Copy after login

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;
}
Copy after login
  • 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
}
Copy after login
  • 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
Copy after login
  • In the command prompt
mkdir –r src\com.educba\com\educba
Copy after login
  • Create a new module in a text editor and save it as a java file module-info.java.
module com.educba
{ }
Copy after login
  • 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
Copy after login

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!

Related labels:
source:php
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!