Skip to content

Lesson 7 of 9 · 26m

The Module System: Strong Boundaries for the Few Cases That Need Them

Understand module declarations, exports, requires, service binding, and when jars on the classpath remain the pragmatic answer.

The Java Platform Module System has an unfortunate reputation because it arrived with migration pain and then many teams learned they did not need it for ordinary applications. Both things are true. JPMS is not a moral requirement for modern Java. It is a tool for strong encapsulation, smaller runtime images, library API boundaries, and applications whose dependency graph deserves compiler enforcement.

A module declaration is dependency documentation

module-info.java sits at the root of a module source tree:

module com.acme.billing {
    requires transitive com.acme.invoices;
    requires java.sql;
    requires com.fasterxml.jackson.databind;

    exports com.acme.billing.api;
    opens com.acme.billing.config to com.fasterxml.jackson.databind;
}

requires declares a dependency. exports declares public API packages. Packages not exported are strongly encapsulated on the module path. opens permits deep reflection into selected packages, which is often needed by serialization and dependency-injection frameworks.

Export is not the same as public

Before modules, every public type was effectively API. With modules, a public class in a non-exported package is implementation detail. That distinction matters for libraries:

com.acme.http.client      exported API
com.acme.http.internal    not exported, free to change

If you maintain libraries consumed across teams, this alone can justify JPMS. Accidental dependencies on implementation packages stop being invisible coupling.

Compilation is part of the model

The exact layout matters. module-info.java is not metadata pasted beside code; it participates in compilation. Build tools hide some of this, but not enough to excuse never learning the command-line model.

Service binding beats classpath scanning

Modules can declare service use and provision explicitly.

This is more predictable than scanning every jar for magic files, especially when producing custom runtime images. It is also more explicit, which means migrations expose assumptions that classpath scanning hid.

One practical payoff is a reduced runtime image:

jlink   --module-path "$JAVA_HOME/jmods:out"   --add-modules com.acme.billing   --output target/runtime   --strip-debug   --no-man-pages   --no-header-files

For small services and command-line tools, shipping only required JDK modules can reduce image size and attack surface. For a large Spring service in a container already pinned to a JDK base image, the benefit may not justify the operational complexity.

Know when not to modularize

Do not convert every application module in a monorepo into a JPMS module because packages feel untidy. Start with packages and build modules. Use JPMS for libraries with public API boundaries, plugin systems, jlink runtimes, or codebases that already suffer from implementation-package coupling. If reflection-heavy frameworks fight every boundary, adjust selectively rather than declaring ideological war.

Field notes

  • requires transitive means consumers see that dependency too; use it deliberately for API-exposed types.
  • Automatic modules derive names from jar metadata or filenames; unstable names are migration hazards.
  • Split packages across modules are rejected and often reveal a broken project layout.
  • opens is for reflection, not for ordinary compile-time access.
  • JPMS does not replace dependency-version management; a module graph is not a vulnerability scanner.

Was this lesson helpful?

Sign in to rate