JPMS and module boundaries
What you will learn
JPMS groups packages into named modules with explicit dependencies and public boundaries. It complements ordinary Java access modifiers rather than replacing them. Start by inspecting the module that defines a familiar class.
- Identify named and unnamed modules.
- Explain readability, exports and opens.
- Compile a two-module service example and diagnose a hidden package.
Each example is a complete Main.java file. Use a fresh lab-owned directory: javac --release 21 -proc:none Main.java, then java Main. Use only trusted local classes and fixed synthetic data. For module commands use the companion lab described below. Never load unknown native code or attach to another process. The runner enforces a process deadline.
Inspect named and unnamed modules
public class Main {
public static void main(String[] args) throws Exception { Module app = Main.class.getModule(); Module base = String.class.getModule(); System.out.println(app.isNamed()); System.out.println(base.getName()); }}Expected output:
falsejava.baseThe classpath example has no module descriptor; String belongs to java.base.
Readability and access
A named module declares dependencies in module-info.java. A module automatically reads java.base. requires lab.api; makes the API module readable; exports lab.api; exposes that package’s public types to other modules that read it. requires transitive lab.api; also grants readability to modules reading the declaring module. opens lab.model to lab.mapper; permits deep reflection by the named recipient, not normal compile-time access. Avoid blanket opening. A module is an encapsulation boundary, not a sandbox.
Build the service graph
The companion lab-fixtures/modules-v36 contains sources for lab.api (Greeting), lab.provider (implementation) and lab.app (consumer). The app declares uses lab.api.Greeting;, the provider declares provides lab.api.Greeting with lab.provider.GreetingImpl;. The provider need not export its implementation. From a clean lab directory, run javac --release 21 -d mods --module-source-path src $(find src -name "*.java") and java --module-path mods --module lab.app/lab.app.Main. Expected: Hello, ByteMind. The verifier also checks rejected imports of the unexported package and denied deep reflection. Its negative compilation test grants only readability to the test consumer (not exports), isolating the missing-export failure. These diagnostic flags are not deployment fixes.
Migration without surprises
Classpath classes normally belong to an unnamed module. An ordinary JAR on the module path can become an automatic module, with a name from its manifest or derived from the filename and broad exports/readability. Prefer explicit descriptors for new libraries. Do not mix multiple providers of the same package in a resolved module graph: split packages can cause compilation or resolution failure. Qualified exports restrict recipients; service binding expresses behavior through an interface. The module path is not a recursive classpath search.
Check exported versus opened packages
public class Main {
public static void main(String[] args) throws Exception { Module base = String.class.getModule(); System.out.println(base.isExported("java.lang")); System.out.println(base.isOpen("java.lang")); }}Expected output:
truefalsePublic API access does not imply deep reflective access. These results assume no extra JVM access flags.
Inspect a module descriptor
public class Main {
public static void main(String[] args) throws Exception { var descriptor = java.sql.Connection.class.getModule().getDescriptor(); System.out.println(descriptor.name()); System.out.println(descriptor.requires().stream().anyMatch(r -> r.name().equals("java.base"))); }}Expected output:
java.sqltrueThe descriptor represents the module declaration, including its mandated base dependency.
Common mistakes
Do not confuse public classes with exported packages, assume requires alone grants access, use opens as a normal import mechanism, or treat JPMS as a security sandbox.
Practice
Print the module name of java.util.logging.Logger and whether it exports java.util.logging. Then run the companion service graph and explain why the provider package is not exported.
Expected output:
java.loggingtrueCompare your answer with the manual practice criteria.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JPMS and module boundaries — knowledge check
Original module, packaging and native-interface questions.
What does requires establish?
What does exports expose?
What permits deep reflection by a named consumer?
Where does an ordinary classpath class usually belong?
How does a module declare a service implementation?