Skip to main content
Java 9 · Topic 1 of 6

Modules (JPMS)

Definition​

The Java Platform Module System (Java 9) groups packages into named modules. A module-info.java file says what the module needs (requires) and which packages other modules may use (exports), so internal packages stay hidden.

Example code​

A module that exports one package and keeps another internal

// file: com.shop/module-info.java
module com.shop {
exports com.shop.api; // other modules may use this package
// com.shop.internal is not exported: it stays private to the module
}

// file: com.shop/com/shop/api/Greeter.java
package com.shop.api;

public class Greeter {
public static String greet() { return com.shop.internal.Helper.hello(); }
public static void main(String[] args) { System.out.println(greet()); }
}

// file: com.shop/com/shop/internal/Helper.java
package com.shop.internal;

public class Helper {
public static String hello() { return "Hello from inside the module"; }
}

// compile and run:
// javac -d out --module-source-path . -m com.shop
// java --module-path out -m com.shop/com.shop.api.Greeter

// Output:
// Hello from inside the module

Finished this page? Track your progress (saved only in your browser).