Lesson 5 of 8
Java Platform Module System (JPMS)
JPMS (Java 9+) introduces modules - a higher-level unit of encapsulation above packages. A module declares which packages it needs (requires) and which it exposes (exports). JPMS enables strong encapsulation, reliable configuration, and smaller application images.
module-info.java - Declaring a Module
// File: module-info.java (root of src/main/java)
// Declares the module and its dependencies
module com.priygop.library {
// Packages you need from other modules
requires java.base; // implicitly required - core Java
requires java.logging; // java.util.logging
requires java.sql; // JDBC - required for database access
requires com.fasterxml.jackson.databind; // Jackson JSON library
// Optional dependency (compile-time only - like Lombok)
requires static java.compiler;
// What packages YOU expose to other modules
exports com.priygop.library.model; // Book, Genre, BorrowRecord
exports com.priygop.library.service; // LibraryService, CheckoutService
// Internal packages - NOT exported (strong encapsulation)
// com.priygop.library.internal → only usable within THIS module
// Expose to specific modules only (qualified exports)
exports com.priygop.library.internal to com.priygop.library.tests;
// Reflection access (for frameworks like Jackson, Hibernate, Spring)
opens com.priygop.library.model to com.fasterxml.jackson.databind;
// 'opens' allows deep reflection - 'exports' allows only compile-time type access
}
// Compilation with modules:
// javac --module-source-path src -d out -m com.priygop.library
// java --module-path out -m com.priygop.library/com.priygop.library.Main
// Multi-module Maven project structure:
// library-app/
// ├── library-model/ (module: com.priygop.library.model)
// │ └── src/main/java/
// │ └── module-info.java
// ├── library-service/ (module: com.priygop.library.service)
// │ └── src/main/java/
// │ └── module-info.java
// └── library-app/ (main module)
// └── src/main/java/
// └── module-info.javaJPMS Benefits & Caveats
- Strong encapsulation - internal packages are truly inaccessible (compile + runtime). Unlike 'package-private' which is bypassed by reflection.
- Reliable configuration - missing modules detected at startup, not runtime. No more NoClassDefFoundError surprises.
- jlink - build a custom JRE image with only the modules your app uses. Smaller deployments (50MB vs 300MB full JDK).
- Caveat: most frameworks (Spring, Hibernate) still have issues with JPMS strict encapsulation - they rely on reflection. Use opens carefully.
- Caveat: switching an existing large project to JPMS is a significant undertaking - unnamed module still exists for classpath code.
- Automatic modules - JARs without module-info.java on module-path become automatic modules (their name derived from JAR filename).
Quick Quiz
Tip
Tip
Practice Java Platform Module System JPMS in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and concepts for Java Platform Module System (JPMS)
Practice Task
Note
Practice Task - (1) Write a working example of Java Platform Module System JPMS from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Common Mistake
Warning
A common mistake with Java Platform Module System JPMS is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready java code.
Key Takeaways
- JPMS (Java 9+) introduces modules - a higher-level unit of encapsulation above packages.
- Strong encapsulation - internal packages are truly inaccessible (compile + runtime). Unlike 'package-private' which is bypassed by reflection.
- Reliable configuration - missing modules detected at startup, not runtime. No more NoClassDefFoundError surprises.
- jlink - build a custom JRE image with only the modules your app uses. Smaller deployments (50MB vs 300MB full JDK).
Finished this lesson?