Skip to content

ZMelliti/java-modules-cheat-sheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 

Repository files navigation

🧠 Java Modules (JPMS) Cheat Sheet

πŸ”Ή What Is a Module?

A Java module is a self-contained unit of code that groups related packages, resources, and configuration.
Introduced in Java 9, modules improve encapsulation, performance, and maintainability.


πŸ—οΈ Basic Syntax: module-info.java

module com.example.myapp {
    requires com.example.utils;
    exports com.example.myapp.api;
}

πŸ“¦ Common JPMS Keywords

Keyword Description
module Declares a module
requires Declares a dependency on another module
exports Makes a package accessible to other modules
opens Allows runtime reflection access (e.g., by frameworks like Jackson)
uses Declares a service interface this module will consume
provides ... with Declares an implementation of a service interface
requires transitive Re-exports a dependency to downstream modules
requires static Marks a compile-time-only dependency (not needed at runtime)
exports ... to Makes a package accessible to specific modules only

πŸ“ Typical Module Structure

my-module/
β”œβ”€β”€ src/
β”‚   └── main/
β”‚       └── java/
β”‚           β”œβ”€β”€ module-info.java
β”‚           └── com/mycompany/mypackage/
β”‚               └── MyClass.java

βœ… exports vs opens

Feature exports opens
Compile-time use βœ” Required ❌ Not accessible
Reflection support ❌ Not allowed βœ” Accessible via reflection
Use case Public APIs JSON mapping, ORM, frameworks, etc.

πŸ” Services (SPI)

Step 1: Define a service interface

// module: payment.api
package com.mycompany.payment;

public interface PaymentGateway {
    void pay(String customer, double amount);
}

Step 2: Provide implementation

// module: payment.impl
module payment.impl {
    requires payment.api;
    provides com.mycompany.payment.PaymentGateway
        with com.mycompany.payment.impl.StripePaymentGateway;
}

Step 3: Load with ServiceLoader

// module: checkout
ServiceLoader<PaymentGateway> loader = ServiceLoader.load(PaymentGateway.class);
loader.findFirst().ifPresent(gw -> gw.pay("Zied", 100));

πŸ“‹ Best Practices

  • βœ… Start modularizing early in new projects
  • βœ… Keep one responsibility per module
  • βœ… Export only what you intend to expose
  • βœ… Use opens only for framework-based reflection
  • βœ… Use provides/uses for pluggable architecture
  • βœ… Test each module independently

⚠️ Common Pitfalls

  • ❌ Forgetting to export a package needed by other modules
  • ❌ Attempting reflection on closed packages (without opens)
  • ❌ Cyclic dependencies between modules
  • ❌ Mixing modules with legacy classpath without control

πŸ§ͺ Compile & Run

Compile:

javac -d out --module-source-path src $(find . -name "*.java")

Run:

java --module-path out -m my.module/com.mycompany.Main

πŸ“š Resources


About

Java Modules Cheat Sheet (JPMS)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published