Command Palette

Search for a command to run...

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

Which module is this class in, and what may it see?

Your class-path code is in the unnamed module. java.base requires nothing: it is the root every other module depends on.

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?

Which module is this class in, and what may it see? Java 9+
Sign in to run this example in your browser.

Expected output

Main is in a named module: false
String and List live in: java.base, java.base
java.base exports java.util: true
java.base exports jdk.internal.misc: false
java.base opens java.lang for reflection: false
my module reads java.base: true
java.base requires: []
blocked: class Main cannot access class jdk.internal.misc.Unsafe (in module java.base) because module java.base does not export jdk.internal.misc