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.
Write a program that prints the module name of String, java.util.logging.Logger and your own Main class ("unnamed" if it has none).
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.
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
What problem does the module system solve that packages and access modifiers couldn't?
What is the difference between exports and opens?
What happens to code on the class path in a modular JDK?
What does requires transitive mean, and when should you use it?
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]