AI Features

Java Module System

Learn to structure large applications, enforce strong encapsulation, and explicitly define dependencies using the Java Platform Module System (JPMS).

For decades, Java applications relied on the classpath, a flat list of JAR files where every public class was visible to every other class. This often led to JAR hell, where missing dependencies caused runtime crashes, or conflicting library versions led to unpredictable behavior.

In this lesson, we will explore the Java Platform Module System (JPMS), introduced in Java 9, which transforms Java from a flat collection of packages into a structured graph of modules. By explicitly defining what our code exposes and what it requires, we gain reliable configuration and strong encapsulation, ensuring that our applications are safer, more maintainable, and easier to scale.

The need for modules

Before modules, Java’s access control had a significant gap. We could use private to hide members within a class and package-private to hide classes within a package, but we had no standard way to hide an entire package within a JAR file. If a class was public, it was accessible to everyone on the classpath, often leading developers to accidentally use internal APIs that were never meant to be exposed.

The module system solves this by introducing a higher level of aggregation: the module. A module is a named, self-describing collection of code and data. It acts as a container for packages and resources, enforcing two critical rules: ...