Sub topic 3
Declaring dependencies with `requires`
This chapter assumes you know Java packages, access modifiers, javac, java, JAR files, and the basic purpose of interfaces. You will use requires to declare which named modules a module needs.
What `requires` means
A module declaration is stored in module-info.java. The requires directive declares a dependency on another named module:
module com.example.app {
requires com.example.greeter;
}This declaration affects both compilation and execution. The compiler allows code in com.example.app to use exported packages from com.example.greeter. At runtime, the module system checks that the required module is present and readable.
A dependency does not expose every class in the required module. The required module must also export the package containing the class.
A two-module example
The provider module exports its public API:
// src/com.example.greeter/module-info.java
module com.example.greeter {
exports com.example.greeter.api;
}
// src/com.example.greeter/com/example/greeter/api/Greeter.java
package com.example.greeter.api;
public class Greeter {
public String greet(String name) {
return "Hello, " + name;
}
}The application requires that module and imports its exported class:
// 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("modules"));
}
}Compile and run the application from the project directory:
javac -d out --module-source-path src -m com.example.app
java --module-path out -m com.example.app/com.example.app.MainThe -m option selects a module. The application module causes javac and java to locate com.example.greeter because it declares requires.
Variants of `requires`
| Directive | Meaning | Typical use |
|---|---|---|
| `requires name` | Adds a normal compile-time and runtime dependency | Most application dependencies |
| `requires transitive name` | Also makes the dependency readable to modules that require this module | A dependency appears in your public API |
| `requires static name` | Required during compilation but optional at runtime | Optional tools or annotation processors |
Use requires transitive sparingly. It expands your module's public dependency graph. If your public methods return java.sql types, for example, consumers may need that module to compile against your API:
module com.example.library {
requires transitive java.sql;
exports com.example.library.api;
}A requires static dependency must not be needed by normal runtime code. If runtime execution reaches a class from that module, the application can fail with a missing-module error.
Reading the dependency graph
Each requires edge gives the declaring module readability of the required module. Readability allows the compiler and runtime to resolve accessible types. It does not bypass package exports or access modifiers.
Exercise
If com.example.app requires com.example.greeter, but the greeter module does not export com.example.greeter.api, can the app import Greeter from that package?
Show answer
No. requires creates readability, but the package must also be exported.