Java 9 - Module System

Java 9, a new kind of programming component called module has been introduced. A module is a self-describing collection of code and data and has a name to identify it.
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.
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.
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.
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.
JMOD format introduced, a packaging format (similar to JAR) which can include native code and configuration files.
Following the steps to create a module say com.tutorialspoint.greetings.
Create a folder C:\>JAVA\src. Now create a folder com.tutorialspoint.greetings which is same as the name of module we're creating.
Create module-info.java in C:\>JAVA\src\com.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.
Add the source code in the module. Create Java9Tester.java in C:\>JAVA\src\com.tutorialspoint.greetings\com\ tutorialspoint\greetings 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.
Create a folder C:\>JAVA\mods. 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

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!


    16 Lectures 
     2 hours 

 Malhar Lathkar

    19 Lectures 
     5 hours 

 Malhar Lathkar

    25 Lectures 
     2.5 hours 

 Anadi Sharma

    126 Lectures 
     7 hours 

  Tushar Kale

    119 Lectures 
     17.5 hours 

 Monica Mittal

    76 Lectures 
     7 hours 

 Arnab Chakraborty
 Print
 Add Notes
 Bookmark this page