Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java Modules - Java Platform Module System

Java Platform Module System is a new feature added into Java 9 version. It allows us to collect related packages and files into a single unit called a module.

Before Java 9, there was no concept of the module system that leads to increase the application size and difficult to move around. But in Java 9, the whole JDK or Java APIs have been restructured into a set of modules so that we can use only the required module for our project. Java Module System is a higher level of aggregation above Java packages.

What is Java Modules?

Java modules is a way of aggregating or categorizing the Java APIs available in the Java core platform, so that when you are developing a new Java application you can only include the required Java libraries in your project, rather than having the complete Java platform APIs.

Java Modules can be System Modules or Application Modules, modules that are related to Java Core such as the Java SE and JDK are known as system modules, and modules that are designed to use modules and defined in the compiled module-info.class are called application modules.

Since JDK 9 is built upon modular approach, we can check the built-in module by using the --list-modules command into terminal or cmd.

$ java --list-modules

The above command will populate the result like below that shows the available modules.

java.base@9.0.8
java.compiler@9.0.8
java.datatransfer@9.0.8
java.desktop@9.0.8
java.instrument@9.0.8
java.logging@9.0.8
java.management@9.0.8
java.management.rmi@9.0.8
java.naming@9.0.8
java.net.http@9.0.8
java.prefs@9.0.8
java.rmi@9.0.8
java.scripting@9.0.8
...

According to Java Specification, the key goals of modularizing the Java SE platform are

  • Reliable configuration
  • Strong encapsulation
  • Scalable Java platform
  • Greater platform integrity
  • Improved performance

Apart from JDK, Java also allows us to create our own modules so that we can develop a module-based application.

To use a module, including the jar file into modulepath instead of the classpath. A modular jar file added to classpath is a normal jar file and the module-info.class file will be ignored.

How to Create Your own Module?

Apart from built-in modules, Java allows us to create user-defined modules. Here, we will learn to create Java module our own by using these simple steps:

1. Create a directory that represents our module.

2. Create a Module Description file as module-info.java.

3. Java source code file.

For example, let's create a folder java9 that contains java.modules and it itself contains a module-info.java (module-descriptive) file and a Java source file as Hello.java.

Arrange these files into this directory structure.

java9/
    -java.modules
        -Hello.java
        -module-info.java

// Hello.java

package com.studytonight;
 
public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello, Modules!");
    }
}

Module-Description file (module-info.java)

module java.modules {
    requires java.base;  
    
}

The module descriptor file specifies the following point:

  • Module’s name
  • Module’s dependencies
  • Module's visibility: specify the packages it explicitly makes available to other modules.
  • Services that it offers
  • Services that it consumes
  • Reflection permission to other modules

The module name in the module-info file should be the same as the module name directory(java.modules), See the directory structure above.

How to Compile Modular Java Source File?

We used the following command to compile our Java module.

javac --module-source-path java9/ -d java9/module-src -m java.modules

--module-source-path is a flag that represents the module location. Since our module is located in the java9 folder, we used java9 in the command.

-d represents the directory to store our compiled module files. module-src is a directory in which .class files will be stored.

-m points to the module name.

After successfully compiling the code. It will be stored into a separate directory and if we see its directory structure then it looks like:

java9/
    -java.modules
        -Hello.java
        -module-info.java
    -module-src
        -java.modules
            -com
                -studytonight
                        -Hello.class
            -module-info.class

How to Execute(Run) the Java Module?

After successfully compile the Java code, now let's run the code and check whether our code produces the desired result. Use the below command.

java --module-path java9/module-src/ -m java.modules/com.studytonight.Hello

After successful execution of the module, we get the result:


Hello, Modules!