Sub topic 3
Declaring dependencies with requires
This chapter assumes you can write a basic module-info.java, know what a package and a public type are, and can run javac and java from a terminal. You should also know what the module path is, at the level of "a place where the tools look for modules." Here you learn how one module states that it needs another, what the compiler and runtime do with that statement, and which errors appear when it is missing or wrong.
What a requires directive says
A requires directive in module-info.java names another module that the current module depends on. It takes a module name, not a package name and not a jar file name.
// src/com.example.app/module-info.java
module com.example.app {
requires com.example.greeter;
}This one line has three effects:
- Resolution. The module system must find
com.example.greeterand include it in the module graph. If it can't, compilation or startup fails. - Readability.
com.example.appnow readscom.example.greeter. Without that edge, code in the app can't refer to any type from the greeter, even if the type ispublic. - Ordering. The required module is initialized and resolved as a dependency, so the runtime knows what has to be present.
Reading a module is necessary but not sufficient. The target module must also export the package you want to use. requires opens the door from your side. exports opens it from the other side.
A complete example
The greeter module exports one package.
// 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 app module requires it and uses the 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 both modules from one source tree and run the app:
javac -d out --module-source-path src -m com.example.app
java --module-path out -m com.example.app/com.example.app.MainThe -m com.example.app option tells javac to compile that module. Because the app requires the greeter, and the greeter is found in the module source path, javac compiles it too and writes each module into its own directory under out.
How resolution works
The module system starts from one or more root modules. For java -m com.example.app/..., the root is com.example.app. It then follows every requires directive, collecting each required module, until no new modules appear. The result is the module graph.
Two points follow from this model:
- Only modules reachable from the roots are in the graph. A module sitting on the module path that nobody requires is not loaded. If you need it anyway, for example because code finds it only through reflection, add it with
--add-modules. - The graph is checked before any application code runs. A missing dependency fails at startup, not when a class is first used. This is the reliable-configuration benefit that the classpath can't give you.
The implicit requires java.base
Every module reads java.base. It holds java.lang, java.util, java.io and the other core packages. You never write requires java.base, and the compiler adds it for you. If you do write it, that is allowed, but it is redundant.
Other JDK modules are different. If you use java.sql or java.logging, you must require them.
module com.example.reports {
requires java.sql;
requires java.logging;
}Readability is not transitive
Suppose module A requires B, and B requires C. A does not read C. A can use C's types only if A also writes requires C.
This matters when B's public API exposes C's types. Say B has a method that returns a java.sql.Connection. A can call that method, but A can't name Connection unless A requires java.sql. A later chapter covers requires transitive, which lets B pass its readability of C on to its own clients. For now, treat the rule as: declare every module whose types your code names directly.
Modifiers in brief
requires accepts two modifiers. Both are covered fully later, but you will meet them in real descriptors.
requires transitive Mmakes every module that reads you also readM.requires static MmakesMmandatory at compile time and optional at run time. Use it for annotation libraries or tools your code only needs while compiling.
Modifiers can be combined, as in requires static transitive, but you should rarely need that.
Rules and restrictions
- Names must match exactly. The name in
requiresis the module's declared name, not its jar name.requires greeter;does not findcom.example.greeter. - No cycles. If A requires B and B requires A, the compiler reports a cyclic dependence. Break the cycle by moving shared types into a third module, or by using services (covered later).
- No duplicates. Requiring the same module twice in one descriptor is a compile error.
- Order does not matter. The directives form a set, not a sequence.
- Split dependencies are the module's concern. The
requiresdirective does not select a version. The module system has no version resolution, so choosing which jar provides a module is a build-tool job.
Common errors
| Where it appears | Message (abbreviated) | Cause and fix |
|---|---|---|
| `javac` | `module not found: com.example.greeter` | The module isn't in the module source path or module path. Add it to `--module-source-path` or `--module-path`, or fix the spelling. |
| `javac` | `package com.example.greeter.api is not visible ... but module com.example.app does not read it` | The code uses a type but the descriptor has no `requires`. Add the directive. |
| `javac` | `package ... is declared in module ..., which does not export it` | The `requires` is present, but the target module doesn't export the package. Ask the owner to export it, or use a different API. |
| `javac` | `cyclic dependence involving ...` | Two modules require each other, directly or through others. Extract the shared code. |
| `java` | `FindException: Module com.example.greeter not found, required by com.example.app` | Compile succeeded, but the run command's `--module-path` lacks the module. Add the directory or jar. |
The first and fifth rows show the same missing module at two different times. A module path that worked for javac is not automatically the one you passed to java.
Inspecting the result
To check what a module declares, ask the runtime:
java --module-path out --describe-module com.example.appThe output lists the module's requires entries, including the mandated java.base. It is a quick way to confirm that a built jar or directory contains the descriptor you expect.
Exercise
Module com.example.reports uses java.sql.Connection in its code and has this descriptor: module com.example.reports { }. It compiles with the error "package java.sql is not visible". What single line fixes it, and why was java.util.List not affected?
Show answer
Add requires java.sql; inside the module body. java.util belongs to java.base, which every module reads implicitly, but java.sql is a separate JDK module that must be required explicitly.
Exercise
Module A requires B, and B requires C. A's code calls new C().run() and fails to compile. Name two ways to make it compile, and say which one is the better default.
Show answer
Either add requires C; to A's descriptor, or have B declare requires transitive C; so that A reads C through B. Adding requires C; to A is the better default, because A names C's types directly and so should state that dependency itself. requires transitive fits when C's types appear in B's public API.