Sub topic 5
Readability and the module graph
This chapter assumes you can write a basic module-info.java with requires and exports, and that you can compile with javac and run with java --module-path. Here you will learn how the requires declarations of many modules combine into one graph, and how that graph decides which code can see which code.
Readability is a directed edge
When module A declares requires B, the module system records that A reads B. Readability is a directed relationship between two modules. It has these properties:
- It is not symmetric. A reading B does not mean B reads A.
- It is not transitive. If A reads B and B reads C, A does not read C.
- Every module reads itself and
java.base. You never writerequires java.base. - A named module cannot read the unnamed module, which holds classpath code. Chapter 13 covers that case.
Readability is only half of access. For code in module A to use a type in module B, three conditions must hold:
- A reads B.
- B exports the type's package, either to everyone or to A specifically.
- The type, or the member you use, is
public(orprotectedfor a subclass).
If any one fails, the compiler rejects the code, and the JVM rejects it at run time.
The module graph
The set of modules and their readability edges is the module graph. The compiler builds it when it compiles, and the runtime builds it again at startup. Take three modules:
// src/com.example.text/module-info.java
module com.example.text {
exports com.example.text.api;
}
// src/com.example.text/com/example/text/api/Shout.java
package com.example.text.api;
public class Shout {
public static String of(String s) {
return s.toUpperCase() + "!";
}
}// src/com.example.greeter/module-info.java
module com.example.greeter {
requires com.example.text;
exports com.example.greeter.api;
}
// src/com.example.greeter/com/example/greeter/api/Greeter.java
package com.example.greeter.api;
import com.example.text.api.Shout;
public class Greeter {
public String greet(String name) {
return Shout.of("hello, " + name);
}
}// src/com.example.app/module-info.java
module com.example.app {
requires com.example.greeter;
}
// src/com.example.app/com/example/app/Main.java
package com.example.app;
import com.example.greeter.api.Greeter;
public class Main {
public static void main(String[] args) {
System.out.println(new Greeter().greet("graph"));
}
}Readability is not transitive
The greeter module uses Shout, but the app module only requires the greeter. Suppose Main tries to call Shout directly:
package com.example.app;
import com.example.text.api.Shout;
public class Main {
public static void main(String[] args) {
System.out.println(Shout.of("direct"));
}
}Compilation fails with a message like this:
error: package com.example.text.api is not visible
(package com.example.text.api is declared in module com.example.text, but module com.example.app does not read it)The text module does export the package, so the export is not the problem. The app module has no edge to the text module. The fix is to add requires com.example.text; to the app descriptor. Do this when the app really uses that module's API directly. Do not rely on the greeter to bring it in.
Resolution: how the graph is built
Resolution is the process that builds the graph at startup. It starts from one or more root modules. When you run java -m com.example.app/..., the root is com.example.app. The runtime then does the following:
- It looks up each root module on the module path, and in the JDK's own modules.
- For every
requiresit finds, it locates the named module and adds it to the graph. - It repeats until no new modules appear.
- It checks the graph for consistency and fails if the check does not pass.
Modules that are on the module path but not reachable from a root are left out. This is why a jar can sit on the path and still never be loaded. You can watch resolution happen:
java --module-path out --show-module-resolution -m com.example.app/com.example.app.MainThe output lists each module as it is resolved, along with the requires that caused it. To see which modules a single module requires, use:
java --module-path out --describe-module com.example.greeterYou can add roots with --add-modules. This matters for modules that nothing requires directly, such as service providers (Chapter 12).
What resolution checks
Resolution fails early, before main runs, when the graph is not valid. The common failures are:
| Problem | When it appears | Typical message |
|---|---|---|
| A required module is missing | Compile time, or startup | `module not found: com.example.text` (compile), or `FindException: Module com.example.text not found, required by com.example.greeter` (startup) |
| Cyclic dependence | Compile time | `cyclic dependence involving com.example.a` |
| Two modules in the graph contain the same package | Startup | `Module X contains package p, module Y exports package p to X` |
| Two modules with the same name on the path | Startup | `Two versions of module ... found` |
Cycles deserve a note. Module A cannot require B while B requires A. Break the cycle by moving the shared types into a third module that both depend on, or by introducing an interface and connecting the two with services.
Checking the graph from the outside
After compiling, you can inspect the declared dependencies of each module without running anything:
javac -d out --module-source-path src -m com.example.app
java --module-path out --describe-module com.example.appThe output includes lines such as requires com.example.greeter and requires java.base mandated. The word mandated marks the implicit java.base edge that you never wrote.
Exercise
A module com.example.app requires com.example.greeter. The greeter module requires com.example.text and exports only com.example.greeter.api. The text module exports com.example.text.api. Can code in com.example.app import com.example.text.api.Shout, and what must you change if you want it to?
Show answer
No. The app module does not read the text module, because readability is not transitive. The export from the text module is not enough. Add requires com.example.text; to the app module's descriptor, and the import will compile.
Exercise
You run java --module-path out -m com.example.app/com.example.app.Main, and a jar for com.example.text is in the out directory but nothing requires it. Is the module loaded?
Show answer
No. Resolution starts at the root module and follows requires edges. A module on the path that is not reachable from a root, and not added with --add-modules, is left out of the graph.