Java module system
On this page 8 topics
Overview
Topics
- JPMS fundamentals
- classpath vs module path
Learn first
1.5 h of practice (estimate)
Learning outcomes
- Write
module-info.javawithrequires,exports, andopens. - Explain the classpath vs the module path and strong encapsulation.
- Decide whether a Spring service benefits from JPMS, and use alternatives (e.g., ArchUnit) for module boundaries.
Lessons
Lab and exercises
Lab
MID-010-LAB1 · Boundaries
Outcomes: 1–3 · Time: ~1.5 h Modularize Shop Core with JPMS (descriptor, exported API package, opened model package if needed). Enforce Shop API's feature boundaries with ArchUnit tests. Introduce a forbidden dependency and show the build failing. Acceptance: a forbidden dependency fails the build.
Exercises (assessment: quiz plus lab)
| ID | Outcome | Task |
|---|---|---|
| MID-010-EX01 | 1 | Write a module-info.java for com.shop.core that requires java.sql and exports one package. |
| MID-010-EX02 | 1 | Add opens ... to for a model package used by Jackson; explain the error without it. |
| MID-010-EX03 | 2 | Explain classpath vs module path and strong encapsulation in four sentences, with an example of what encapsulation blocks. |
| MID-010-EX04 | 2 | Compile a class that uses an unexported package from another module; read the error. |
| MID-010-EX05 | 3 | For Shop API (Spring Boot), argue for or against JPMS and name one alternative. |
| MID-010-EX06 | 3 | Write an ArchUnit rule: services must not depend on controllers. |
Show solutions
- EX01
module com.shop.core { requires java.sql; exports com.shop.core.api; } - EX02 Without
opens, reflection throwsInaccessibleObjectExceptionfor private-member access; addopens com.shop.core.model to com.fasterxml.jackson.databind;. - EX03 Classpath: flat, everything public is visible. Module path: only exported packages of required modules are visible. Encapsulation blocks access to internals even if
public(and deep reflection unless opened). - EX04 The compiler reports the package is not visible or not exported.
- EX05 Typical answer: against for the Boot service (reflection-heavy, library gaps, high cost); use ArchUnit or build modules for boundaries. A reasoned answer for a library is acceptable.
- EX06
noClasses().that().resideInAPackage("..service..").should().dependOnClassesThat().resideInAPackage("..controller..")with@AnalyzeClasses(verify against the ArchUnit version).
Quiz
Outcomes: O1 module descriptors · O2 classpath vs module path · O3 JPMS decision and alternatives.
A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.
Practical part
Six exercises MID-010-EX01 to EX06 plus the lab. Pass: at least five exercises correct, eight of ten quiz questions right, and the lab where a forbidden dependency fails the build.
Common mistakes
Adopting JPMS for frameworks that rely on deep reflection without opens.
Assessment
Quiz plus lab.
Career relevance
Modular monolith boundaries (MID-059).