Command Palette

Search for a command to run...

Back to the lesson: Topic 15.3 — The Module System (JPMS)
Core Java · Example 2 of 2 Java 9+

A module descriptor, built in code

The builder added "requires mandated java.base" by itself, exactly as javac does for every module-info.java.

The Java Platform Module System (JPMS, Java 9) groups packages into named modules described by a module-info.java file. A module states which modules it requires and which packages it exports; everything else is hidden, even from reflection, which gives libraries real encapsulation, reliable startup checks and small custom runtimes with jlink.

Change the code and press Run (Ctrl+Enter). Try to predict the output first, then break it on purpose and read the error. Your edits are saved and match the lesson page.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write a program that prints the module name of String, java.util.logging.Logger and your own Main class ("unnamed" if it has none).

02

Write the module-info.java for a library com.acme.cache that exposes com.acme.cache.api, hides com.acme.cache.impl, depends on java.logging, and allows Jackson to reflect on com.acme.cache.api.dto.

03

Using ModuleDescriptor.newModule, build a module named demo that exports demo.api, and print whether build() added java.base to its requires.

Explain it without notes

01

What problem does the module system solve that packages and access modifiers couldn't?

02

What is the difference between exports and opens?

03

What happens to code on the class path in a modular JDK?

04

What does requires transitive mean, and when should you use it?

A module descriptor, built in code Java 9+
Sign in to run this example in your browser.

Expected output

module com.shop.orders {
    requires java.sql
    requires mandated java.base
    requires transitive com.shop.money
    exports com.shop.orders.api
    exports com.shop.orders.spi to [com.shop.payments]
    opens com.shop.orders.model
    uses com.shop.orders.spi.PricingRule
}
all packages: [com.shop.orders.api, com.shop.orders.internal, com.shop.orders.model, com.shop.orders.spi]