Sub topic 6
Modules reached through requires transitive
This chapter assumes you can write and compile basic Java classes and that you know single-type and on-demand imports. It also assumes you have seen import module once, as in import module java.base;. You don't need to have written a module-info.java file. The chapter explains the one directive it needs, requires transitive.
By the end you should be able to predict which packages a single import module line brings in. Some of those packages belong to modules you never named.
Recap: what requires transitive does
A module declaration lists the modules it depends on with requires. A plain requires gives the declaring module readability of the other module. It can use that module's exported packages. The dependency stays private to the declaring module. Any module that reads you does not automatically read your dependencies.
requires transitive changes that. If module A declares requires transitive B, then every module that reads A also reads B. This is called implied readability. Authors use it when B's types appear in A's public API. A caller of A then needs B to compile against A.
java.sql is the standard example. Its API exposes java.util.logging.Logger, the XML types used by SQLXML, and javax.transaction.xa.XAException. Its descriptor therefore looks like this (shortened):
module java.sql {
requires transitive java.logging;
requires transitive java.transaction.xa;
requires transitive java.xml;
exports java.sql;
exports javax.sql;
}Every module that writes requires java.sql; can also use the logging, XA and XML packages without declaring them.
The rule for module imports
A module import declaration takes its meaning from the same readability graph. The language rule says that import module M; imports on demand every public top-level class and interface in:
- the packages that
Mexports to the current module, and - the packages exported to the current module by every module that
Mrequires transitively.
Rule 2 is recursive. If M has requires transitive N, and N has requires transitive P, then the types of M, N and P are all imported. The traversal follows only requires transitive edges. A plain requires edge ends the walk.
Two details follow from the wording:
- Only exported packages count. Internal packages of
Mor of its transitive modules stay invisible. - Only public top-level types count. Nested types, such as
Map.Entry, are not imported by name. Package-private classes are never imported.
The imports behave like on-demand imports, so they do not load or link anything at runtime. They only change how simple names resolve at compile time.
A worked example
The following program imports java.base and java.sql. It uses a class from java.sql, a class from java.logging and several classes from java.xml. Only java.sql appears in an import declaration of the kind that matters here.
import module java.base;
import module java.sql;
public class TransitiveDemo {
private static final Logger LOG = Logger.getLogger("demo");
public static void main(String[] args) throws Exception {
LOG.info("starting");
Timestamp ts = Timestamp.valueOf("2026-01-01 10:00:00");
System.out.println(ts);
DocumentBuilder builder =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader("<a><b/></a>")));
System.out.println(doc.getDocumentElement().getTagName());
}
}Each simple name comes from a different place:
| Simple name | Package | Module that exports it | How it was reached |
|---|---|---|---|
| `Timestamp` | `java.sql` | `java.sql` | named directly |
| `Logger` | `java.util.logging` | `java.logging` | `java.sql` requires it transitively |
| `DocumentBuilder`, `DocumentBuilderFactory` | `javax.xml.parsers` | `java.xml` | `java.sql` requires it transitively |
| `Document` | `org.w3c.dom` | `java.xml` | `java.sql` requires it transitively |
| `InputSource` | `org.xml.sax` | `java.xml` | `java.sql` requires it transitively |
| `StringReader` | `java.io` | `java.base` | `import module java.base;` |
The program prints a timestamp such as 2026-01-01 10:00:00.0 and then a. The logger writes an INFO line to standard error.
Without a module import you would need single-type or on-demand imports for six packages. The import module line replaces all of them.
What is not reached
The first surprise is java.base. Every module has an implicit, mandated requires java.base. It is not transitive. So import module java.sql; does not give you List, Map or StringReader. That is why the example above imports java.base on its own line.
The second case is a plain requires. Suppose a library declares:
module com.example.lib {
requires transitive com.example.model;
requires com.example.util;
exports com.example.lib.api;
}Then import module com.example.lib; imports the public types of com.example.lib.api and of every package exported by com.example.model. It does not import anything from com.example.util, even if the library uses util heavily. That module is an implementation detail. If you want its types, import it yourself:
import module com.example.lib;
import module com.example.util;This matches how requires already works for ordinary code. The import declaration does not reveal anything that readability would hide.
Inspecting a module's transitive edges
You do not have to guess which edges are transitive. The java launcher can print a module descriptor:
java --describe-module java.sql
java --describe-module java.seIn the output, look for lines that start with requires transitive. Lines that start with a plain requires mark the edges that module imports do not follow. For a module on your module path, use --module-path <path> with --describe-module, or run jar --describe-module --file <jar> on a modular JAR.
Aggregator modules and large imports
Some modules exist only to group others. The best known is java.se, which requires transitively almost every module that makes up the Java SE API. That includes java.base, java.sql, java.desktop, java.xml, java.net.http and many more. So a single line:
import module java.se;imports essentially the whole Java SE API by simple name. This is legal and sometimes convenient for a throwaway script. It also maximizes the chance of a name clash, because one module group contains java.util.List and another contains java.awt.List. The following code does not compile:
import module java.se;
public class Clash {
public static void main(String[] args) {
List<String> names = null; // error: reference to List is ambiguous
}
}The two on-demand sources, java.util.List through java.base and java.awt.List through java.desktop, collide on use. The fixes from the earlier chapter apply: add a single-type import such as import java.util.List;, or write the fully qualified name. The error appears only where the ambiguous name is used. Importing both modules without using List compiles fine.
Using module imports inside a named module
If your code belongs to a named module, the compiler enforces readability. Your module must read M before you can write import module M;. A module that does not read M gets a compile error on the import line.
module com.example.app {
requires java.sql;
}With this descriptor, source files in com.example.app may write import module java.sql;. They may also use Logger or Document from the transitive modules, even though the descriptor never mentions java.logging or java.xml. Implied readability gives the module that access.
The descriptor above does not let the files write import module java.base; in an unusual way. It is allowed, because every module reads java.base. It is also redundant, because the java.lang package is the only part of java.base that Java imports for you automatically, and everything else needs an import.
Two mistakes come up in named modules:
- Writing
import module java.net.http;withoutrequires java.net.http;in the descriptor. The fix is to add therequiresdirective. The import declaration never adds a dependency. - Depending on a transitive module for code that outlives the dependency. If you rely on
java.sqlto supplyjava.loggingand a later JDK stops marking that edge transitive, your imports break. For long-lived modules that use a transitive module's types heavily, add an explicitrequiresfor it.
In the unnamed module (ordinary class-path code and single-file launches), the module being imported must be present in the module graph. The default root modules include the standard Java SE ones, so import module java.sql; works. A non-JDK module on the module path may need --add-modules.
Compiling and running
The behavior is the same on every JDK where the feature exists. Only the flags differ:
# JDK 25 and later (feature is final)
javac --release 25 TransitiveDemo.java
java TransitiveDemo
# JDK 23 (first preview)
javac --release 23 --enable-preview TransitiveDemo.java
java --enable-preview TransitiveDemoOn JDK 24, which carried the second preview, use the same flags with --release 24. On JDK 22 and earlier, import module is a syntax error. You can also run a source file directly with java TransitiveDemo.java on JDK 25. On a preview release, add --enable-preview --source <N>.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `cannot find symbol` for `List` after `import module java.sql;` | `java.base` is not transitive | add `import module java.base;` |
| `reference to X is ambiguous` for a name you never imported | a transitive module exports a package with the same simple name | add a single-type import or use the qualified name |
| `package ... does not read module` or `module not found` on the import line | the named module does not `require` the module | add `requires` to `module-info.java` |
| Types from a library's helper module are missing | the library uses plain `requires`, not `requires transitive` | add `import module` for the helper module, or use a normal import |
| Error that mentions preview features | compiled on JDK 23 or 24 without flags | add `--enable-preview` and `--release` |
Exercises
Exercise
A module lib declares requires transitive model; and requires util;. Your code says import module lib;. Which of the two modules' exported types can you use by simple name, and why?
Show answer
Types exported by lib and by model can be used. The module import follows requires transitive edges, so model is included. util is a plain requires, so its types are not imported. You would need import module util; or ordinary imports for them.
Exercise
The program has only import module java.sql; and uses ArrayList<String>. It fails with "cannot find symbol". Give two ways to make it compile.
Show answer
java.base is not reached through requires transitive from java.sql. Either add import module java.base; or add the single-type import import java.util.ArrayList;.
Exercise
A named module com.example.app has requires java.sql; in its descriptor. Can a class in it use import module java.sql; and also write Logger.getLogger("x") with no further imports or requires? Explain.
Show answer
Yes. The module reads java.sql, so the import is allowed. java.sql declares requires transitive java.logging, so java.util.logging.Logger is imported by the same declaration. Implied readability means the descriptor does not need to name java.logging.