logo

Module System


Show

It is the new way of programming components named modules that have been introduced in Java 9. A module is the own-defined collection of code and information(Data) and has a name to recognize it.

Features:

The below-given enhancements have been added in Java 9 with the Module Component-

  • A new optional phase link time is introduced. This phase is in the middle of the compile-time and run time. A set of modules may be assembled and optimized during this phase. Also making a custom runtime image using the jlink tool.
  • Javac,jlink, and java have supplementary options to define module paths. Which after that locate definitions of modules.
  • JAR format is being updated as modular JAR, which contains module-info.class file class in its root directory.
  • JMOD Format is introduced, a packaging format (same as JAR) that can comprise native codes and configuration files.

Creating Module

Below are the steps to create a Module named com.intellinuts.greetings

Step-1

Create a folder C:\>JAVA\src. Now make a folder com.intellinuts.greetings and it is the same as the module we are creating.

Step-2

Create a module-info.java in C:\>JAVA\src\com.intellinuts.greetings folder with the given code.

Module-info.java

module com.intellinuts.greetings { }

Module-info.java is the file which is used to make modules. We have created a module named com.intellinuts.greetings in this step. By convention, this file must reside in the folder whose name is the same as the module name.

Step-3

Now, we have to add the source code in the module. Create a Java9Testter.java in C:\>JAVA\src\com.intellinuts.greetings\com\intellinuts\greetings folder with the help of following code.

Java9Tester.java

package com.intellinuts.greetings;

public class Java9Tester {
   public static void main(String[] args) {
      System.out.println("Hello World!");
   }
}

By convention, a source code of a module will lie in the same directory which is the name of the module.
Step-4

Create a folder C:\>JAVA\mods. Now, create a folder com.intellinuts.greetings and it is the same as the name of the module we have created. Now assemble the module to the mods directory.

C:/ > JAVA > javac -d mods/com.intellinuts.greetings 
   src/com.intellinuts.greetings/module-info.java 
   src/com.intellinuts.greetings/com/intellinuts/greetings/Java9Tester.java

Step-5

Let’s run the module to see the result. Right the below-given command.

C:/>JAVA>java --module-path mods -mcom.intellinuts.greetings/com.intellinuts.greetings.Java9Tester

Here module path gives the module location as mods and -m signifies the main module.

Output

You will see the output like this:

Hello World!