Java Module System: Modularize Your Applications with Java 9+
Explore the Java Module System (JPMS), introduced in Java 9 to enhance modularity in Java applications. Understand the concept of modules as self-contained collections of code and resources, allowing developers to encapsulate functionality, restrict access, and streamline large applications. Learn how Java's modular platform structure improves code organization and security.
Java Module System
In Java 9, the module system was introduced to improve modularity in Java applications. The module system is also known as JPMS, Java Platform Module System, and is commonly referred to as "Modules".
What is a Module?
A module is a self-contained collection of code and data that is identified by a unique name. It contains packages, resources, and configurations related to a specific functionality. Modules allow developers to restrict access to the packages they contain. By default, code in a module's package is not visible to the outside world, not even via reflection.
From Java 9 onwards, the Java platform itself is modularized. To list the modules supported by Java, you can use the --list-modules
command:
Syntax
C:\Users\JohnDoe>java --list-modules
java.base@20.0.2
java.compiler@20.0.2
java.sql@20.0.2
jdk.javadoc@20.0.2
... (list continues)
Output
java.base@20.0.2
java.compiler@20.0.2
java.sql@20.0.2
jdk.javadoc@20.0.2
... (list continues)
Modules like jdk
are for JDK-specific packages, while java
modules are for standard library packages.
Features of Java Module System
The module system in Java 9 introduced several enhancements, such as:
- A new phase called "link time," which is optional and occurs between compile time and runtime. During this phase, modules can be assembled and optimized into a custom runtime image using the
jlink
tool. - Additional options in
javac
,jlink
, andjava
to specify module paths, allowing modules to be located more efficiently. - Modular JARs, which include a
module-info.class
file in the root directory. - The introduction of the JMOD format, which is similar to JAR but can include native code and configuration files.
Declaring a Module
To declare a module, you need to create a module-info.java
file in the root folder of your application. This file contains the metadata for the module.
Syntax
module com.example.greetings {
}
Adding Dependent Modules
You can declare dependencies of other modules in the module descriptor. For example, if you want to use the com.example.util
module, you can add it as follows:
Syntax
module com.example.greetings {
requires com.example.util;
}
Adding Optional Modules
To declare optional dependencies, use the static
keyword:
Syntax
module com.example.greetings {
requires com.example.util;
requires static com.example.logging;
}
Adding Transitive Modules
Transitive dependencies allow you to expose dependencies to modules that require your module. Use the transitive
keyword:
Syntax
module com.example.greetings {
requires com.example.util;
requires transitive com.example.base;
}
Exporting Public Classes
By default, no public classes in a module's package are accessible to the outside. You must export the package to make the public classes available:
Syntax
module com.example.greetings {
exports com.example.greetings.HelloWorld;
}
Allowing Reflection
To allow reflection for accessing private members of a module, use the opens
keyword:
Syntax
module com.example.greetings {
opens com.example.greetings.HelloWorld;
}
Creating and Using a Java Module
Step 1: Create a Module Directory
Create a directory for the module: C:/JAVA/src/com/example/greetings
.
Step 2: Create module-info.java
In the module folder, create a module-info.java
file with the following content:
Syntax
module com.example.greetings {
}
Step 3: Add Source Code
Create a Java9Tester.java
file inside the module folder:
Syntax
package com.example.greetings;
public class Java9Tester {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Step 4: Compile the Module
Compile the module using the following command:
Syntax
C:/JAVA> javac -d mods/com.example.greetings src/com.example.greetings/module-info.java src/com.example.greetings/com/example/greetings/Java9Tester.java
Step 5: Run the Module
Run the compiled module using the command:
Syntax
C:/JAVA> java --module-path mods -m com.example.greetings/com.example.greetings.Java9Tester
Output
Hello World!