CoursesJVM internals for Kotlin and Android developers

Puzzle 16 · 17 of 27

Can the JVM check before main runs?

Puzzle 15's missing class was found only at its first use, an hour into the run. Put the same App and Helper into named groups that declare what they need, and see the JVM refuse to start instead.

All puzzles in this course

The problem

In puzzle 15, App printed start and then died at the first call to Helper, because the class path is only a list of places to search. Nothing on it says which classes the program will need. Could the program declare what it depends on, so that the JVM can check before running a single line?

See it

Left: the module path mods with module app, whose module-info requires bonus, and module bonus. A startup check follows the requires line to bonus. With bonus present, main runs and prints start and 3. With bonus removed, the check fails with FindException: Module bonus not found, required by app, and main never runs module path: mods apprequires bonus bonusHelper java -p mods -m app/…App startup check bonus foundmain: start3 ? missingFindException: Module bonus not found, required by appmain never runs

Scroll sideways to see the full diagram.

  1. Two Java modules, app and bonus, compiled into the folder mods. The module path, given with -p, lists where the modules are. app's module-info says requires bonus.

    Step 1 of 5
  2. Before main runs, the JVM reads app's requires line and looks for bonus on the module path.

    Step 2 of 5
  3. bonus is there, so the check passes. main runs and prints start, then Helper.bonus() returns 3.

    Step 3 of 5
  4. Now remove the bonus folder and run the same command. Predict: does main print start?

    Step 4 of 5
  5. No. The check fails first: FindException, Module bonus not found, required by app. Not one line of main runs.

    Step 5 of 5

The building block

Since JDK 9, Java code can be grouped into Java modules (not to be confused with this course's modules, its groups of puzzles). A Java module is a named set of packages with a small file, module-info.java, at its top that says which other modules it requires and which of its own packages it exports to others. Here are App and Helper again, now in two modules, app and bonus:

src/
  app/
    module-info.java
    dev/pachulski/app/App.java
  bonus/
    module-info.java
    dev/pachulski/bonus/Helper.java
// src/app/module-info.java
module app {
    requires bonus;
}

// src/bonus/module-info.java
module bonus {
    exports dev.pachulski.bonus;
}
// src/app/dev/pachulski/app/App.java
package dev.pachulski.app;

import dev.pachulski.bonus.Helper;

public class App {
    public static void main(String[] args) {
        System.out.println("start");
        System.out.println(Helper.bonus());
    }
}

// src/bonus/dev/pachulski/bonus/Helper.java
package dev.pachulski.bonus;

public class Helper {
    public static int bonus() { return 3; }
}

Modules are loaded from the module path, given with -p (or --module-path), instead of the class path. -m names the module and the class whose main should run. With both modules present, it prints start and 3. Now remove the compiled bonus module and run it again:

$ java -p mods -m app/dev.pachulski.app.App
Error occurred during initialization of boot layer
java.lang.module.FindException: Module bonus not found, required by app

start is not printed. Before running main, the JVM reads the requires lines, starting from the module you named and following each required module's own requires lines, and checks that every required module is there, the way the module system's design calls "resolving". A missing piece is found at startup, not an hour later at its first use. That is the module system's stated goal of "reliable configuration": the class path just lists places to search, so, in the design document's words, it's "impossible to tell, in advance, when an artifact is missing".

class path (puzzle 15)module path
What the JVM knows at startupa list of folders and jarseach module and what it requires
A missing piece is foundat the first use of a class from itbefore main runs
What you seeNoClassDefFoundErrorFindException: Module bonus not found

The check covers modules, not every class. If bonus also held a second class, Rate, that Helper.bonus() calls, and only Rate.class were deleted, the module would still be found: the run prints start and then fails at the call with NoClassDefFoundError: dev/pachulski/bonus/Rate, exactly as on the class path. (Delete Helper.class instead, the only class of the package bonus exports, and the module itself is broken: the JVM stops at startup with FindException: Error reading module.)

The edges

  • The same classes can still run from the class path. java -cp mods/app:mods/bonus dev.pachulski.app.App runs the same compiled code with no module check at all: it prints start and 3, and with bonus missing it would fail at first use, as in puzzle 15. Classes on the class path belong to what the module system calls the unnamed module.
  • Most apps you meet still use the class path. Anything started with -cp does.
  • Exports matter at compile time too. Only packages a module exports can be used by modules that require it; javac refuses code that reaches into a package that isn't exported.

The analogy

Puzzle 15's kitchen opens and finds out a dish's ingredient is missing only when someone orders it. A module-path kitchen has every recipe card list its ingredients, and the manager checks the whole list before unlocking the door: if one is missing, the kitchen doesn't open at all, and the sign says which ingredient and which recipe needed it.

Run it yourself

Create the four files above in that layout, then:

javac -d mods --module-source-path src -m app,bonus
java -p mods -m app/dev.pachulski.app.App
mv mods/bonus bonus-away
java -p mods -m app/dev.pachulski.app.App

Before the last command, predict whether start is printed.

Check yourself

The module app requires bonus, and bonus requires a third module, json, which is missing. App's main doesn't touch json at all. Does start print?

Right. Startup resolution covers every module reachable through requires.

Not quite. That is the class path's behavior; the module path checks first. Re-read that part ↑

Not quite. Resolving follows each required module's own requires lines too.

Why can the module path fail before main, while the class path can't?

Not quite. Speed isn't the difference; the class path doesn't say what the program needs.

Right. A declared dependency can be checked; a search list can't.

Not quite. Only the modules are checked; classes still load when first needed.

A service starts with java -cp app.jar:json.jar, and json.jar is missing on one server. When does it fail?

Not quite. -cp is the class path, which checks nothing at startup. Re-read that part ↑

Right. Only the module path checks at startup.

Not quite. Nothing substitutes a missing class; its first use fails.

Sources
  • The State of the Module System ("the specific goals of the module system are to provide Reliable configuration , to replace the brittle, error-prone class-path mechanism with a means for program components to declare explicit dependences upon one another"; "This makes it impossible to tell, in advance, when an artifact is missing."; "If a request is made to load a type whose package is not defined in any known module then the module system will attempt to load it from the class path. If this succeeds then the type is considered to be a member of a special module known as the unnamed module")
  • JEP 261: Module System ("The module system constructs a module graph by resolving the transitive closure of the dependences of a set of root modules with respect to the set of observable modules."; release: 9)
  • The java command (JDK 21) ("--module-path modulepath ... or -p modulepath Specifies where to find application modules with a list of path elements."; "To launch the main class in a module: java [ options ] -m module [ / mainclass ] [ args ...]")
  • The module runs (start and 3; FindException with bonus removed; start and 3 from the class path) were produced in this session with OpenJDK 21.0.12.

What you now know

A Java module is a named set of packages whose module-info file says which other modules it requires. On the module path, the JVM resolves every requires line before main runs, so a missing module stops the program at startup instead of failing at first use.