Java Tutorial

Modules (JPMS)

Java Platform Module System: Organizing large codebases (Java 9+)

What are Java Modules?

Introduced in Java 9, the Java Platform Module System (JPMS) is a major update to how Java code is organized and compiled. Prior to this, Java relied on the classpath, which was slow and prone to "JAR hell" (where different libraries randomly conflict with each other).
Modules solve these problems by allowing you to export specific packages to the outside world and require only the modules your code actually needs.
java
// module-info.java (The Module Descriptor!)
module com.mygame.physics {
    requires java.sql;      // I need the database!
    exports com.mygame.api; // Let others use my API!
}

Comparisons: Classpath vs. Module Path

Wait! Why did we add modules after 20 years of Java development?
  • Classpath: "Flat" logic. If a class is found, it's used. No privacy between JARs.
  • Module Path: "Structural" logic. Every module defines exactly what it shows and what it uses.
FeatureClasspathModule Path
LogicText-based search.Pre-compiled, binary links.
SpeedSlow (Same code re-scanned every time!)FAST (JVM knows exactly where everything is!)
EncapsulationNone. Everything is "Public" to everyone.Strong. Only things with exports are visible!

Creating a Module

  1. Define: Create a file named module-info.java in the root of your package structure.
  2. Declare: Use the module keyword to name your module.
  3. Export: Explicitly list which packages should be visible to other modules.
java
module com.myproject.core {
    // Only 'utils' is visible outside!
    exports com.myproject.core.utils; 
}

Why Use Modules?

  1. Scalability: Essential for massive enterprise projects with 1,000s of classes.
  2. Security: Internal code (like sensitive data handlers) can be hidden forever.
  3. Lean JRE: You can use the jlink tool to create a tiny "custom Java Runtime" that contains ONLY the modules your app needs. This is perfect for Docker and Cloud deployments!

Summary

TermDescription
moduleA named, self-contained group of code
module-infoThe special file that defines the module
requiresDeclaring a dependency on another module
exportsMaking a package public to everyone
opensAllowing "Reflection" access to a package
providesDeclaring a Service Implementation
usesDeclaring a Service Interface requirement
Best ForEnterprise apps, library development, Cloud/Docker
Java 9The first version with "Project Jigsaw" (Modules!)
SpeedFaster startup times for large-scale apps
Transitiverequires transitive (I need it, and my users will too!)
ModularThe whole Java Standard Library is now modular!
StandardEstablished and trusted worldwide for 10+ years
EncapsulateNo more accessing internal com.sun.* classes!
LibraryMost modern Java libraries (Spring 6+) are modular!
JlinkTool to create a custom, tiny Java Runtime
JmodThe new binary format for compiled modules
StructureModules help keep your architecture "Clean"
ImplicitEvery module implicitly requires java.base!
UnnamedCode on the old classpath is in an "Unnamed Module"
SafetyPrevents "JAR Hell" and conflicting class files
TestingModules make it harder to "cheat" and access private data
RefactorSplitting a single project into many modules
ModernAll development after Java 17 should be modular!