JPMS modules
The Java Platform Module System, added in Java 9, groups packages into modules that declare what they export and what they require. It makes public stop meaning "visible to everyone".
Detailed Explanation
Before Java 9 the only unit of packaging was the JAR, and a JAR is a zip file with no opinions. Every public class inside it was reachable by every other class on the classpath. Library authors had no way to say "these three packages are the API, the other twelve are internals you must not touch". They resorted to naming conventions like com.acme.internal and hoped.
People touched them anyway. The JDK's own sun.misc.Unsafe is the famous example: an internal class that half the ecosystem depended on, which meant the JDK team could not change it. JPMS exists to make encapsulation enforceable rather than advisory.
A module is declared in a file called module-info.java at the root of your source tree. It names the module, lists the packages it exports, and lists the modules it requires. Anything not exported is invisible outside the module at compile time and at run time, even if the class is public. Reflection is blocked too unless you opens the package, which is why frameworks that inject into private fields need that keyword.
The dependency graph is resolved at startup. If module A requires module B and B is absent, the JVM fails immediately with a clear message instead of throwing NoClassDefFoundError an hour into the run. Split packages, where the same package appears in two modules, are rejected outright. That strictness is the feature.
The JDK itself was modularised in the same release. java.base is always required implicitly and holds java.lang, java.util, and the rest of the core. Things like java.sql, java.xml, and java.desktop are separate modules. This is what makes jlink possible: it walks the module graph and builds a runtime image containing only the modules your application reaches, which can cut a container image by well over a hundred megabytes.
Two mechanisms exist rather than one. Code on the module path is modular and enforced. Code on the classpath lands in the unnamed module, which reads everything and exports everything. That unnamed module is the compatibility escape hatch, and it is why the overwhelming majority of Java applications written after 2017 still do not use modules at all. Most Spring Boot services run entirely on the classpath.
requires transitive is worth knowing for interview purposes. If your module's public API returns a type from another module, consumers need that module too, and transitive passes the readability along automatically.
Reach for JPMS when you are publishing a library and want the internals genuinely sealed, or when you are shipping a standalone application and want a minimal runtime via jlink. Skip it for an ordinary server application where your build tool already manages dependencies.
The cost is real friction. Every dependency must be modular or get an automatic module name derived from its JAR, which is fragile. Reflection-heavy frameworks need opens declarations. Strong encapsulation of JDK internals, which became the default in Java 16 and fully enforced in Java 17, broke a generation of libraries. The payoff is reliable configuration, true encapsulation, and small runtime images.
Code Examples
src/com.acme.billing/module-info.java
// src/com.acme.billing/module-info.java
module com.acme.billing {
// We need SQL types in our own implementation.
requires java.sql;
// Consumers that use our API also need java.desktop types we return,
// so pass the readability on instead of making them declare it.
requires transitive java.desktop;
// Only this package is visible outside the module.
exports com.acme.billing.api;
// Everything in com.acme.billing.internal stays private, even public classes.
// Allow a DI framework to reflect into our model package at runtime.
opens com.acme.billing.model to com.fasterxml.jackson.databind;
}package com.acme.billing.api;
public class ModuleIntrospection {
public static void main(String[] args) {
Module self = ModuleIntrospection.class.getModule();
// Named on the module path; unnamed on the plain classpath.
System.out.println(self.getName());
// prints: com.acme.billing (or null when run from the classpath)
System.out.println(String.class.getModule().getName());
// prints: java.base
}
}Common Gotchas
`public` is no longer enough
a public class in a package the module does not `export` cannot be imported from outside. The compiler reports that the package is not visible, which reads as a missing dependency but is not one.
Reflection needs `opens`, not `exports`
`exports` grants compile-time and normal runtime access. Setting a private field reflectively, which Jackson and Hibernate do constantly, requires `opens` or you get `InaccessibleObjectException`.
Automatic module names are a trap for published libraries
a non-modular JAR on the module path gets a name derived from its filename, which changes when the filename changes. If you publish a module, add an `Automatic-Module-Name` manifest entry at minimum.
You can ignore modules entirely
running everything on the classpath keeps pre-9 behaviour. Java 17 still enforces encapsulation of JDK internals though, so `--add-opens` flags may be needed for old libraries regardless of whether your own code is modular.