On this page
1Concept / Definition
A module is a named, self-describing group of packages and resources. Its descriptor, module-info.java at the root of the source tree, states the module's name, which other modules it requires, which of its packages it exports (for compile-time and run-time use) or opens (for deep reflection), and which services it uses or provides. The compiler and JVM enforce these declarations — reliable configuration and strong encapsulation are the two goals of JPMS.

2Why It Exists
Before Java 9, the only unit above the class was the package, and public meant public to the entire class path. Internal implementation packages of libraries and of the JDK (sun.misc.Unsafe, com.sun.*) were routinely used by others, making them impossible to change. The class path had no notion of dependencies, so missing or duplicate JARs surfaced as run-time errors ("JAR hell"). And the JDK was a monolith of ~60 MB of rt.jar shipped to every application. JPMS addresses all three.
3Core Concepts
Module declaration
module com.shop.orders { … } declares a module; the name conventionally follows reverse-DNS like the top-level package. The directives are requires, exports, opens, uses and provides. An open module (open module m { … }) opens all its packages for reflection. The descriptor compiles to module-info.class in the JAR root. Since Java 25, module import declarations (import module java.sql;) let a compilation unit import all packages exported by a module — convenient in scripts and teaching code (JEP 511).

Exports
exports com.shop.orders.api; makes the public types (and their public and protected members) of that package accessible to other modules. Packages not exported are encapsulated: even public classes in them cannot be used from outside, at compile or run time. Qualified exports (exports p to m1, m2;) restrict access to named friend modules — the JDK uses them extensively for internal APIs shared between its own modules.
Requires
requires com.shop.common; declares that this module reads another module and depends on it at compile and run time. requires transitive M gives implied readability: any module that reads this one automatically reads M too — use it when M's types appear in your exported API. requires static M is a compile-time-only dependency, optional at run time (annotation libraries, optional integrations). Every module implicitly requires java.base. Cyclic requires are not allowed.

Opens
opens com.shop.orders.model; grants deep reflective access at run time — setAccessible(true) on private members — without making the package available at compile time. It exists because frameworks such as Jackson, Hibernate, Spring and test libraries need reflection into your classes. Prefer qualified opens (opens p to com.fasterxml.jackson.databind;) to limit who can reflect. exports and opens are independent: a package can be exported, opened, both or neither.


Uses/provides
Services decouple consumers from implementations. The API module defines an interface; a consumer declares uses com.shop.spi.PricingRule; and calls ServiceLoader.load(PricingRule.class); provider modules declare provides com.shop.spi.PricingRule with com.shop.vip.VipDiscount;. The consumer never requires the provider modules — they only need to be present at run time. A provider can supply a public static provider() method instead of a public no-arg constructor. On the class path the older META-INF/services files still work.


4How It Works Internally
At compile time javac builds the module graph and rejects access to non-exported packages and unread modules. At startup the JVM resolves the module graph from the root module (1.2), checks for missing modules, duplicate packages and cycles, and records for each module which packages it exports or opens to whom. Every access check — linking a class reference, reflective access, MethodHandles.Lookup — consults this data. The Module class (Class.getModule()) exposes it at run time, with methods such as isExported, isOpen, addReads and addOpens (the latter callable only by the module itself).
5APIs / Features / Syntax
// module-info.java of an API module
module com.shop.api {
requires transitive java.sql; // java.sql types appear in our API
exports com.shop.api;
exports com.shop.api.spi;
}
// Implementation module
module com.shop.orders {
requires com.shop.api; // gets java.sql implicitly (transitive)
requires static org.jspecify; // annotations only at compile time
exports com.shop.orders; // public API of this module
// JSON binding
opens com.shop.orders.dto to com.fasterxml.jackson.databind;
uses com.shop.api.spi.PricingRule;
provides com.shop.api.spi.Exporter with com.shop.orders.CsvExporter;
}
// Loading services
List<PricingRule> rules = ServiceLoader.load(PricingRule.class).stream()
.map(ServiceLoader.Provider::get).toList();
// Inspecting modules at run time
Module m = Order.class.getModule();
System.out.println(m.getName() + " exports api? " + m.isExported("com.shop.orders"));6Practical Examples
// Compile and run with the module path
// $ javac -d out --module-source-path src $(find src -name '*.java')
// $ java --module-path out:libs -m com.shop.app/com.shop.app.Main
// Inspect dependencies of an existing JAR (also flags JDK-internal API use)
// $ jdeps --multi-release 25 --print-module-deps app.jar
// java.base,java.net.http,java.sql
// Build a minimal runtime image for a container
// $ jlink --add-modules com.shop.app,java.net.http --module-path out:libs \
// --strip-debug --no-header-files --no-man-pages --output /opt/runtime
// Java 25 module import (JEP 511) in a small program
import module java.base;
import module java.net.http;7Common Problems & Mistakes
| Problem | Symptom | Fix |
|---|---|---|
| Package not exported | 'package X is not visible' at compile time | exports p (or qualified) |
| Reflection into non-open package | InaccessibleObjectException | opens p to <framework> |
| API type from a dependency not transitive | Consumers fail to compile | requires transitive |
| Split package (same package in two modules) | Resolution failure at startup | Merge or rename packages |
| Missing uses directive | ServiceLoader finds nothing | Add uses X |
| Opening everything (open module) by habit | Lost encapsulation | Qualified opens for specific packages |
8Performance & Production Considerations
- jlink images with only needed modules shrink container images (often from ~200 MB JDK to 40–70 MB runtimes) and reduce attack surface.
- Module resolution adds negligible startup cost; the archived module graph in the CDS archive speeds it further.
- Split packages and duplicate modules fail at startup rather than causing subtle run-time conflicts — a production benefit.
- Most enterprise applications still run on the class path; they still benefit from the modular JDK and must respect its encapsulation.
9Architecture & Design Considerations
Modules make architectural boundaries enforceable: a module's exported packages are its public API and everything else is private, which is exactly what modular monoliths and layered architectures need. Where full JPMS adoption is impractical (framework constraints, many non-modular libraries), the same boundaries can be enforced with build modules (Maven/Gradle multi-module), ArchUnit rules or Spring Modulith. Services (uses/provides) are a lightweight plug-in mechanism that avoids a DI framework for extension points.
10Interview Questions & Answers
What is a Java module?
A Java module is a named, self-describing unit of code: a set of packages plus a descriptor, module-info.java, at its root. The descriptor declares which other modules it requires, which of its packages it exports to other modules, which packages it opens for reflection, and which services it uses or provides. Unlike a plain jar on the class path, a module has strong encapsulation: packages it does not export are inaccessible to other modules, even if their classes are public. The JDK itself has been split into about 70 modules, such as java.base and java.sql, since Java 9.
What does exports do?
The exports directive makes the public types of one package accessible to other modules, at both compile time and run time. Packages that are not exported stay encapsulated: other modules cannot use their types, even public ones, and the compiler and JVM enforce this. For example, a payments module might export com.shop.payments.api but not com.shop.payments.internal, so callers can only use the published API. Exports are per package, not per class, so design packages around what you want to publish. A qualified export, exports p to m, limits access to named modules only.
What is the difference between exports and opens?
exports gives other modules normal access to the public types and public members of a package, at compile time and run time. opens gives access only at run time, but deep access: reflection can reach all members, including private fields and methods, through setAccessible. Frameworks such as Hibernate, Jackson and Spring need this to populate private fields or call non-public constructors. You can open a package without exporting it, so frameworks can reflect over entity classes while other modules cannot compile against them. An open module opens all its packages, which is convenient but weakens encapsulation.
What does requires transitive do?
requires transitive gives implied readability. If module A declares requires transitive B, then any module that requires A can also read B automatically, without declaring it. You need it when B's types appear in A's public API. For example, if a payments API module has methods that return java.time types from another module, or types from a shared money module, its clients must be able to read that module to compile against the API. Without transitive, every client would have to add the dependency itself. Use it only for dependencies that are truly part of your API, not for internal implementation dependencies.
When would you use requires static?
requires static declares a dependency that is mandatory at compile time but optional at run time. The compiler can see the module, but the JVM does not require it to be present when the application starts. Typical uses are annotation libraries needed only during compilation, such as those for nullness checking or code generation, and optional integrations, such as support for a metrics library that is used only if the application includes it. Code that uses an optional dependency must check that it is available, usually through ServiceLoader or a class presence check, before calling it.
How do uses and provides work?
Services decouple a consumer from its implementations. The consumer module declares uses com.shop.spi.PaymentProvider and obtains implementations at run time with ServiceLoader.load(PaymentProvider.class). Each provider module declares provides com.shop.spi.PaymentProvider with com.acme.UpiProvider, and does not need to export its implementation package. The consumer has no compile-time dependency on any provider, so new providers can be added by putting a module on the module path. This is how the JDK discovers JDBC drivers and charset providers. It gives plug-in architectures with compile-time checking of the service contract.
What are qualified exports and why does the JDK use them?
A qualified export, written exports p to m1, m2, makes a package accessible only to the listed modules, rather than to every module. The JDK uses qualified exports heavily to share internal packages among its own modules without exposing them to applications. For example, internal packages of java.base are exported only to specific JDK modules that need them. In applications, qualified exports let a group of closely related modules, such as a core module and its adapters, share implementation packages while keeping them hidden from everyone else. They should be used sparingly, because they couple the modules involved.
Why are split packages forbidden?
A split package is one whose classes are spread across two modules. The module system forbids it because each package must have exactly one owner: access control, encapsulation and class loading are all decided per package. If two modules contain the same package, the JVM cannot tell which one is authoritative, and resolution fails at start-up. On the class path this was allowed, which caused subtle bugs when two jars contained different versions of the same class. Split packages are a common obstacle when modularising older libraries; the fix is to rename or merge packages so each belongs to one module.
How can modules enforce architecture in a modular monolith?
Treat each bounded context, such as orders, payments or catalogue, as a module that exports only its API package and keeps its implementation packages hidden. Dependencies between contexts are declared explicitly in module-info, must be acyclic, and any code that reaches into another context's internals fails to compile. This turns architecture rules from documentation into build failures. Where full JPMS is not practical, for example because of framework constraints, you can get similar enforcement with separate build modules, ArchUnit tests or Spring Modulith's verification. The goal is boundaries that are cheap to keep, so a module can later be extracted as a service.
How does jlink help containerised deployments?
jlink assembles a custom Java runtime that contains only the modules an application needs, instead of the full JDK. You first find the required modules, for example with jdeps --print-module-deps, then run jlink with --add-modules and options such as --strip-debug, --no-header-files and --no-man-pages. A typical service runtime shrinks from over 300 MB to well under 100 MB. In containers this means smaller images, faster pulls during scaling, less disk I/O at start-up and a smaller attack surface with fewer components to patch. Keep a separate debug image with diagnostic tools so production troubleshooting is still possible.
What would a realistic JPMS adoption strategy look like for a large Spring application?
Be pragmatic and incremental. First upgrade the JDK and remove uses of internal JDK APIs, using jdeps to find them; this alone brings most security and performance benefits. Keep the application itself on the class path, because Spring and many libraries rely on reflection and classpath scanning, but enforce internal boundaries with build modules, ArchUnit or Spring Modulith. Use jlink to build small runtime images. Modularise shared internal libraries where the ecosystem supports it, and adopt full JPMS only where strong encapsulation clearly pays off, such as platform libraries consumed by many teams. Record the approach in an ADR so teams apply it consistently.
Test your knowledge on this topic →Try multiple choice →Flashcards from this topic's 11 questions, filtered by level and type.