The module system

Why 8 to 11 is the hard upgrade, what `public` stopped meaning, and the honest answer to whether you should modularise — usually no.

10 min read✨ Modern Java: 8 to 25

The module system arrived in Java 9, split the JDK into modules, and is the reason the 8 → 11 upgrade is the hard one. Most applications still do not use it for their own code — and every application is affected by it, because the JDK does.

That gap is why this lesson exists: you need to understand modules to upgrade, and you probably do not need to adopt them.

The problem it was built for

Before Java 9 there was one classpath, and it had three failures nobody could fix from inside it:

  • Jar hell. Two versions of the same library on the classpath; the first one found wins; which one is first is the order the classpath happens to be in.
  • No encapsulation across jars. public meant public to everything. A library's internal package was as reachable as its API, so people used it, and the library could never change it.
  • No way to know what was missing. A NoClassDefFoundError at run time, hours in, rather than a failure at start.

The JDK had the same problems in the large: rt.jar was a single monolith, every part of it always present, and nothing could be left out of a deployment.

What a module is

A directory of packages with a file that says what it needs and what it offers:

module-info.javajava
module com.example.orders {
    requires com.example.payments;      // what I need
    requires java.sql;                  // JDK modules are modules too
    exports com.example.orders.api;     // what others may use
    // everything else is INACCESSIBLE, even if it is public
}

That last comment is the whole idea: public no longer means reachable. A public class in a package that is not exported cannot be used from another module, and the compiler and the runtime both enforce it. This is strong encapsulation, and it is the thing the classpath could never give you.

Two directives worth knowing beyond the basics:

  • opens allows reflection at run time without allowing compile-time access — which is what frameworks need. Spring and Hibernate reflect into your classes; opens com.example.orders.model; is how you permit that without exporting the package as API.
  • requires transitive means "and anyone who requires me also gets this", for when your API's types come from another module.

And the pair that makes plugins work without either side naming the other: uses and provides. A module that consumes a service declares uses com.example.spi.Exporter; and asks ServiceLoader.load(Exporter.class) at run time; a module that implements it declares provides com.example.spi.Exporter with com.acme.CsvExporter;. Neither requires the other, so a new exporter is a jar on the module path and nothing else changes. That is how JDBC finds drivers, how java.util.logging finds its handlers, and how Jackson finds its modules; on the classpath the same ServiceLoader reads META-INF/services/<interface> files instead, which is what most libraries still ship.

The classpath and the module path

They coexist, and the rules for how are most of the practical knowledge:

  • Code on the module path is a module, and its module-info is enforced.
  • Code on the classpath goes into the unnamed module, which reads everything and exports everything — that is the compatibility escape hatch, and it is why an application that ignores modules still runs.
  • A plain jar placed on the module path becomes an automatic module: its name derived from the jar or from Automatic-Module-Name in its manifest, requiring everything and exporting everything.

Automatic modules are the bridge, and they are also the trap: the derived name comes from the filename, so a version change can change the module name and break a requires clause. The fix libraries adopted is Automatic-Module-Name in the manifest, which pins it.

The errors you will actually meet

Upgrading past Java 8 produces a small set of messages, and knowing what each means saves a day:

plaintext
error: package javax.xml.bind is not visible

The Java EE modules were removed in Java 11. Add JAXB as an ordinary dependency; it is on Maven Central.

plaintext
InaccessibleObjectException: Unable to make field private ... accessible:
module java.base does not "opens java.util" to unnamed module

Something is reflecting into the JDK. The escape hatch is --add-opens java.base/java.util=ALL-UNNAMED, and it should be a ticket rather than a permanent flag — a library needing it is a library to upgrade.

plaintext
java.lang.NoClassDefFoundError: javax/annotation/PostConstruct

The same removal, arriving at run time instead of compile time.

Should you modularise your own application?

For most services, no, and the reasoning is worth being explicit about rather than treating as laziness.

What it would buy you: enforced internal boundaries, a smaller runtime image via jlink, and errors at start rather than at run time.

What it costs: every dependency must be a module or an automatic module, reflection needs opens for each framework, the build gets more complex, and a large number of libraries still ship without module descriptors.

And the alternative already exists. A multi-module Maven or Gradle build gives you enforced boundaries at compile time — the build fails if a module imports something it did not declare — which is most of the benefit for none of the cost. The build lesson makes exactly this argument: a module boundary turns an architectural rule into one the compiler enforces.

Where JPMS genuinely earns it: a library with a public API and internals it wants to keep, and jlink where a small runtime image matters — a JDK trimmed to the modules you need is tens of megabytes rather than hundreds.

Under the hood: what module-info.java becomes, and how readability is checked

module-info.java is not metadata read by a config parser — it compiles to a real class file, module-info.class, with its own constant-pool structure (Module, ModulePackages, ModuleMainClass attributes) sitting at the root of the jar. At startup, before your main method runs, the JVM's module system reads every module-info.class reachable from the module path and builds the readability graph: a directed graph where an edge from module A to module B means "A reads B", created for every requires clause (plus java.base, which every module reads implicitly, the same way every class implicitly extends Object).

Two checks happen against that graph, and they are different checks with different failure modes:

  • Readability (requires): can module A see module B's exported types at all? Checked once, at module resolution — before any of your code runs — so a missing requires is a FindException or module not found at launch, not a NoClassDefFoundError three requests in.
  • Accessibility (exports): even if A reads B, can A reach a specific package in B? This is enforced per-access, by the class loader and the verifier, and is what makes a non-exported public class throw IllegalAccessError at the exact call site that touches it, compile-time or run-time depending on whether javac or the JVM catches it first.

This is why the "strong" in strong encapsulation is precise: a classpath's public was enforced by nothing but convention — reflection could always reach past it, and setAccessible(true) still can on the classpath's unnamed module, which reads and exports everything to itself. On the module path, setAccessible(true) on a class in a package that is not opened throws InaccessibleObjectException at the reflection call, which is the exact error this lesson's "errors you will actually meet" section shows for java.base's internals — the JVM itself enforcing the graph, not a convention a determined caller can route around.

Try it yourself

Reason through a missing opens

A service's module-info.java has exports com.example.orders.api; but not opens com.example.orders.model;. Hibernate, used for JPA entities in com.example.orders.model, throws InaccessibleObjectException trying to set a private field via reflection at startup. A teammate proposes fixing it by changing exports com.example.orders.api; to also cover the model package. Is that the right fix?

Answer

No — exports and opens answer different questions, and reaching for exports here fixes the symptom by breaking encapsulation instead of by permitting the thing that actually needs it. exports com.example.orders.model; would let any module compile against and call the model package's public types directly — turning internal JPA entities into public API, permanently, for every consumer. What Hibernate needs is narrower: reflective access at run time only, to set private fields via Field.setAccessible(true), with no compile-time visibility implied at all. That is exactly what opens com.example.orders.model; grants — deep reflection for frameworks, without exporting the package as something other code is meant to import against. The fix is adding opens com.example.orders.model; (or opens com.example.orders.model to org.hibernate.orm.core; to open it only to the specific module that needs it), leaving exports untouched.

Misconceptions

  • "A module's requires and its readability are basically the same as a classpath dependency." A classpath dependency is resolved by whatever build tool put jars in the classpath, at whatever order; readability is a graph the JVM itself builds and checks before your code runs, from data compiled into module-info.class, and a missing edge fails at launch rather than producing an inconsistent classpath order.
  • "opens is a weaker version of exports." They are orthogonal, not ordered: exports grants compile-time and run-time type access with no reflection privilege beyond what public API already allows; opens grants deep reflection (including into non-public members) with no compile-time visibility. A package can be exported and not opened (an API frameworks may not reflect into), opened and not exported (an internal model frameworks may reflect into but nobody may import), both, or neither.

Sources

  • JEP 261 (Module System) and JEP 200 (The Modular JDK) — the readability graph, strong encapsulation and the JDK's own modularization.
  • JEP 282 (jlink: The Java Linker) — building a trimmed runtime image from the module graph.
  • The Java Language Specification, §7.7 ("Module Declarations") — requires, exports, opens, uses and provides syntax and semantics.
  • java.lang.module package documentation (ModuleDescriptor, Configuration) — how the JVM resolves and reads the module graph at run time.
Progress is saved on this device and to your account when signed in.