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.

[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]

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

3. Java source code file.

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

Arrange these files into this directory structure.



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

Module-Description file (

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:


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!