”;
In Java 9, Module system was introduced to enhance Java code modularity. Module is an abstraction over package. This module system is also known as JPMS, Java Platform Module System. It is mostly referred as Modules.
What is a Module?
A module is a self-describing collection of code and data and has a name to identify it. It a new kind of programming component which can contains packages, configurations, resources specific to a particular functionality. A module is able to restrict access to packages it contains. By default, code in a package within a module is not visible to outside world, not even via reflection. Java platform is itself modularised from java 9 onwards. If we use the list-modules command to list the java modules, it will print the various modules java supports as following:
C:UsersMahesh>java --list-modules [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] C:UsersMahesh>
Here we can see, that jdk specific packages are in jdk module and library specific packages are in java module.
Features of Java Module System
With the Modules component, following enhancements has been added in Java 9 −
-
A new optional phase, link time is introduced. This phase is in-between compile time and run time. During this phase, a set of modules can be assembled and optimized, making a custom runtime image using jlink tool.
-
javac, jlink, and java have additional options to specify module paths, which further locate definitions of modules.
-
JAR format updated as modular JAR, which contains module-info.class file in its root directory.
-
JMOD format introduced, a packaging format (similar to JAR) which can include native code and configuration files.
Declaring Module
In order to declare a module, we need to create a module-info.java in root folder of the application. This file contains all the meta data or module descriptions.
Example
module-info.java
module com.tutorialspoint.greetings { }
Adding Dependent Modules
We can declare dependencies of other modules in the module. For example, if we want to use com.tutorialspoint.util module then we can add the declaration as follows:
Example
module com.tutorialspoint.greetings { requires com.tutorialspoint.util; }
Adding Optional Modules
We can declare optional dependencies of other modules using static keyword in the module. For example, if we want to use com.tutorialspoint.logging module then we can add the declaration as follows:
Example
module com.tutorialspoint.greetings { requires com.tutorialspoint.util; requires static com.tutorialspoint.logging; }
Adding Transitive Modules
We can declare transitive dependencies of other modules using transitive keyword in the module. For example, if we want to use com.tutorialspoint.base module as dependency of com.tutorialspoint.util module then we can add the declaration as follows:
Example
module com.tutorialspoint.greetings { requires com.tutorialspoint.util; requires static com.tutorialspoint.logging; requires transitive com.tutorialspoint.base; }
This means if a module wants to use com.tutorialspoint.greetings then that module is required to add com.tutorialspoint.base module as well in module declaration.
Export Public Classes
By default, no public class of a package of a module is exposed to outside world. In order to use the public class, we”ve to export it as shown below:
Example
module com.tutorialspoint.greetings { requires com.tutorialspoint.util; requires static com.tutorialspoint.logging; requires transitive com.tutorialspoint.base; exports com.tutorialspoint.greetings.HelloWorld; }
Allow Reflection
By default, no private member of a package of a module is accessible via reflection. In order to allow reflection to inspect the class or module, we”ve to use opens command as shown below:
Example
module com.tutorialspoint.greetings { requires com.tutorialspoint.util; requires static com.tutorialspoint.logging; requires transitive com.tutorialspoint.base; exports com.tutorialspoint.greetings.HelloWorld; opens com.tutorialspoint.greetings.HelloWorld; }
If we need to allow complete module to be open for reflection, we can use open command as shown below:
open module com.tutorialspoint.greetings { requires com.tutorialspoint.util; requires static com.tutorialspoint.logging; requires transitive com.tutorialspoint.base; exports com.tutorialspoint.greetings.HelloWorld; }
Creating and Using Java Module
Following the steps to create a module say com.tutorialspoint.greetings.
Step 1
Create a folder C:>JAVAsrc. Now create a folder com.tutorialspoint.greetings which is same as the name of module we”re creating.
Step 2
Create module-info.java in C:>JAVAsrccom.tutorialspoint.greetings folder with following code.
module-info.java
module com.tutorialspoint.greetings { }
module-info.java is the file which is used to create module. In this step we”ve created a module named com.tutorialspoint.greetings. By convention this file should reside in the folder whose name is same as module name.
Step 3
Add the source code in the module. Create Java9Tester.java in C:>JAVAsrccom.tutorialspoint.greetingscom tutorialspointgreetings folder with following code.
Java9Tester.java
package com.tutorialspoint.greetings; public class Java9Tester { public static void main(String[] args) { System.out.println("Hello World!"); } }
By convention, the source code of a module to lie in same directory which is the name of the module.
Step 4
Create a folder C:>JAVAmods. Now create a folder com.tutorialspoint.greetings which is same as the name of module we”ve created. Now compile the module to mods directory.
C:/ > JAVA > javac -d mods/com.tutorialspoint.greetings src/com.tutorialspoint.greetings/module-info.java src/com.tutorialspoint.greetings/com/tutorialspoint/greetings/Java9Tester.java
Step 5
Let”s run the module to see the result. Run the following command.
C:/>JAVA>java --module-path mods -m com.tutorialspoint.greetings/com.tutorialspoint.greetings.Java9Tester
Here module-path provides the module location as mods and -m signifies the main module.
It will print the following output on console.
Hello World!
”;