Table of Contents
Key Highlights
Java 9 Module System
Types of Java 9 Modules
1. Application modules
2. Automatic Modules
3. Unnamed Module
Modular Descriptor
Modules of Java 9 Module System Application
How to Create a New Java 9 Module?
Comparison Table of JDK 8 and JDK 9
Conclusion
FAQs
Q1. How do I migrate my legacy application into JAVA 9?
 Q2. How do I know which modules are provided by the system?
Q3. Is it necessary to install IDE to work in JAVA?
 Q4. How to access a public package in my module?
Home Java javaTutorial Java 9 Modules

Java 9 Modules

Aug 30, 2024 pm 03:53 PM
java

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1673
14
PHP Tutorial
1277
29
C# Tutorial
1257
24
PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP vs. Python: Use Cases and Applications PHP vs. Python: Use Cases and Applications Apr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

See all articles