Sub topic 1
Why build a custom runtime?
Prerequisites
Assume you can compile and run Java programs with javac and java, use a command line, and understand JAR files and the classpath. This chapter introduces the Java Platform Module System (JPMS), which organizes Java code into named modules.
Why build a custom runtime?
A full JDK contains tools such as javac, documentation utilities, and many libraries that an application may never use. A custom runtime image contains only the modules needed to run one application.
This can reduce distribution size and make deployment more predictable. The image also includes a known Java runtime, so users do not need to install a compatible JDK separately. The trade-off is that you must rebuild the image when you change dependencies or need security updates.
Modules in a nutshell
A module is a named group of packages with explicit dependencies and access rules. The module system reads these rules to construct a reliable dependency graph.
A module usually has a module-info.java descriptor. Unlike a classpath, where dependencies can be implicit and loosely controlled, a module path lets tools inspect module names and relationships.
Declaring dependencies with module-info.java
The requires statement declares a module dependency. The following descriptor says that the application uses the JDK's logging module:
module com.example.app {
requires java.logging;
}exports controls which packages other modules may use. A package that is not exported can remain an internal implementation detail. The java.base module is available automatically to every module, so it normally does not appear in the descriptor.
The modules inside the JDK
The JDK is divided into modules. java.base provides core classes such as String, collections, and threads. Other modules provide focused APIs.
| Module | Typical contents |
|---|---|
| `java.logging` | The `java.util.logging` API |
| `java.sql` | JDBC database access |
| `java.desktop` | AWT, Swing, and desktop integration |
The jlink tool combines selected JDK modules with application modules to create an image.
The module path and the jmods directory
A module path is a list of locations containing modular JARs or exploded modules. The JDK also provides compiled module definitions in its jmods directory. The java.base.jmod file, for example, describes the base module.
When building an image, jlink searches both your application modules and the JDK's jmods directory. The path separator is : on Unix-like systems and ; on Windows.
Exercise
Why does a modular application declare requires java.logging instead of relying on the classpath?
Show answer
The declaration makes the dependency explicit, allowing the module system and jlink to resolve and include it.