Skip to main content
Advertisement

The Java Module System

Starting with Java 9, the Module System, born from Project Jigsaw, was officially introduced. The Module System evolves beyond the traditional structure based purely on packages and JAR files, enabling applications to be built in smaller, more secure, and highly encapsulated components.

1. Why Was the Module System Introduced?

In previous Java versions, any class declared public inside a JAR file could be accessed from anywhere—even by completely unrelated external JAR files. In massive systems, this led to severe dependency management and security issues (often referred to as "JAR Hell"). Moreover, the Java Runtime Environment (JRE) itself had become incredibly large and monolithic, making it unsuitable for deployment on IoT devices or very small system environments.

To address this, the Java Module System provides a higher-level abstraction called a Module, which groups related packages together securely.

2. What is a Module?

A module is essentially a container for packages. Within a single module, closely related packages are bundled together. More importantly, it clearly declares which specific packages it exposes to the outside world (exports), and exactly which external modules it depends upon to function properly (requires).

3. The module-info.java File

To define a module, you must create a module-info.java file at the root directory of your project and configure the module's properties inside it.

// Example of a module-info.java file

module com.myapp.core {
// 1. Specify which packages are exposed to other modules (hidden by default)
exports com.myapp.core.utils;
exports com.myapp.core.services;

// 2. Specify the external modules this module requires
requires java.sql; // Standard underlying database module
requires java.logging; // Logging module

// 3. Expose classes that provide implementations for a specific interface (SPI)
provides com.myapp.core.spi.MyService with com.myapp.core.impl.MyServiceImpl;
}

Changes in Access Control

Once the module system is applied, even if a class is declared as public, it absolutely cannot be accessed by an external module unless the package containing that class is explicitly exported in the module-info.java file. This allows core internal logic to be perfectly encapsulated and hidden.

4. Advantages of the Module System

  1. Strong Encapsulation: Critical internal APIs are hidden securely, and only intended, public APIs are exposed at the module level.
  2. Reliable Configuration: It verifies that a module has all its required dependencies fully loaded before execution, avoiding abrupt missing class errors down the line.
  3. Scalable Runtime (Custom JRE): Using tools like jlink, you can package only the standard Java modules your application truly needs into an ultra-lightweight, customized runtime environment. This is highly beneficial for memory and storage management in Container environments like Docker.

Conclusion

The module system is incredibly powerful for large-scale library developers and massive enterprise systems. While smaller, standard web projects heavily managed by frameworks (like Spring Boot) might not require you to write modules explicitly daily, understanding this mechanism is essential for navigating the modern backend platform ecosystem.

Advertisement