All topics
library
advanced

Java Platform Module System (JPMS)

Understand Java modules: module-info.java, exports, requires, and strong encapsulation.

The Java Platform Module System (JPMS, aka Jigsaw, Java 9) adds a higher-level encapsulation above packages. A module is a group of packages with a module-info.java that declares:

Modules = apartments in a building. Each apartment (module) has a front door (exports) and private rooms (internal packages). The building directory (module-info) says who can visit whom.

Key Concepts

1
requires — which modules this module depends on exports — which packages are accessible to other modules opens — which packages are accessible to reflection (but not compile-time code) provides/uses — service provider interfaces (SPI)
2
Before JPMS, any public class was accessible from anywhere (split packages, classpath hell). JPMS enforces: - Strong encapsulation: even public classes in non-exported packages are inaccessible - Reliable configuration: missing dependencies are detected at startup, not at first use - No split packages: two modules can't contain the same package
3
The JDK itself is modularized: java.base (always available), java.sql, java.net.http, etc. You can create custom runtime images with jlink containing only the modules you need.
4
Modulepath vs classpath: code on the modulepath respects module boundaries. Code on the classpath is in the 'unnamed module' which can access all exported packages.
5
Adoption has been slow for applications (most still use classpath), but the JDK modularization improved performance and security.