Sub topic 2
Recap: Modules, Exports and Readability
This chapter assumes you can write and compile ordinary Java classes and know what a package and a fully qualified name are. It also assumes you have used import declarations. You don't need prior module experience. This chapter covers the parts of the module system that import module builds on.
Why modules exist
Before Java 9, a JAR was a folder of packages. Any public class in any package on the classpath could be used by any other code. Library authors had no way to say "this package is for internal use". Applications ended up depending on internal classes, and the runtime could not check whether a dependency was missing until a class failed to load.
A module is a named group of packages with a declared contract. The contract is written in a file named module-info.java at the root of the module's source. It says two things:
- Which other modules this module needs (
requires). - Which of its packages other modules may use (
exports).
The compiler and the runtime both enforce this contract.
Anatomy of a module declaration
A module declaration looks like this:
module com.example.greeter {
exports com.example.greeter.api;
}The module name, com.example.greeter, follows the same reverse-domain convention as packages, but it is a separate namespace. The module contains every package in its source tree. Only com.example.greeter.api is exported.
The directives you will meet most often are in the table below.
| Directive | Meaning |
|---|---|
| `requires m;` | This module depends on module `m` and can read it. |
| `requires transitive m;` | Same, and every module that reads this module also reads `m`. |
| `exports p;` | Public types in package `p` are accessible to every module that reads this one. |
| `exports p to m1, m2;` | Same, but only for the listed modules (a qualified export). |
| `opens p;` | Allows deep reflection into `p` at run time. This chapter does not cover it. |
Every module implicitly has requires java.base;. You never write it. java.base contains the packages almost every program uses, such as java.lang, java.util, java.util.stream, java.io, java.nio.file and java.time.
Readability and accessibility are two separate checks
The module system separates two ideas.
- Readability. Module A reads module B if A has a
requiresfor B (directly, or through arequires transitivechain). The set of all these relationships is the module graph. - Accessibility. Code in module A can use a type from module B only if all three of these hold:
- A reads B.
- B exports the package that contains the type.
- The type is
public, or isprotectedand accessed from a subclass.
If any of the three fails, the compiler reports an error. The public modifier on a class is no longer enough on its own. This is called strong encapsulation.
Here is a small two-module project that shows all three checks. The layout is:
src/
com.example.greeter/
module-info.java
com/example/greeter/api/Greeter.java
com/example/greeter/internal/PoliteGreeter.java
com.example.app/
module-info.java
com/example/app/Main.javaThe greeter module exports its api package and keeps internal private.
// 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;
import com.example.greeter.internal.PoliteGreeter;
public interface Greeter {
String greet(String name);
static Greeter create() {
return new PoliteGreeter();
}
}// src/com.example.greeter/com/example/greeter/internal/PoliteGreeter.java
package com.example.greeter.internal;
import com.example.greeter.api.Greeter;
public class PoliteGreeter implements Greeter {
@Override
public String greet(String name) {
return "Hello, " + name + ". Pleased to meet you.";
}
}PoliteGreeter is public, but its package is not exported. Code outside the module cannot name it. Code inside the module can use it freely, as Greeter does.
The app module reads the greeter module:
// 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) {
Greeter greeter = Greeter.create();
System.out.println(greeter.greet("Ada"));
}
}Compile and run it from the project root. With --module-source-path, javac compiles the named module and everything it requires:
javac -d out --module-source-path src --module com.example.app
java --module-path out --module com.example.app/com.example.app.MainThe program prints Hello, Ada. Pleased to meet you.
The two classic errors
Each of the three accessibility checks produces its own error message. Suppose Main tries to use the internal class:
import com.example.greeter.internal.PoliteGreeter;The compiler rejects it with a message of this shape:
package com.example.greeter.internal is not visible
(package com.example.greeter.internal is declared in module com.example.greeter,
which does not export it)The class is public and the app module reads the greeter module, but the export check fails.
Now remove requires com.example.greeter; from the app's module-info.java. Even the import of com.example.greeter.api.Greeter fails, with this message:
package com.example.greeter.api is not visible
(package com.example.greeter.api is declared in module com.example.greeter,
but module com.example.app does not read it)Now the readability check fails, even though the package is exported.
| Symptom | Failed check | Fix |
|---|---|---|
| "does not export it" | The package is not in an `exports` directive | Export the package if it is meant to be public API. Otherwise stop using it. |
| "does not read it" | No `requires` path from your module to the target | Add `requires` for the module. |
| "cannot find symbol" on a type in an exported package | The type is not `public` | Make it public, or use a different type. |
Readability can be transitive
Suppose a method in module B returns a type from module C. Every module that calls that method needs to read C as well, or it cannot name the return type. Making each caller write requires C is tedious and fragile.
requires transitive solves this. If module B declares requires transitive C;, then any module that reads B also reads C. This is called implied readability.
The JDK uses this heavily. The java.sql module declares these dependencies:
module java.sql {
requires transitive java.logging;
requires transitive java.transaction.xa;
requires transitive java.xml;
// ... exports of java.sql and javax.sql
}This is a simplified view of the real declaration. A module that writes only requires java.sql; therefore also reads java.logging, java.xml and java.transaction.xa. You can check any JDK module's declaration from the command line:
java --list-modules
java --describe-module java.sqlThe --describe-module output lists exports, requires and requires transitive lines. Lines marked transitive are the ones that extend your readability.
The diagram below shows the readability graph for an app that requires java.sql.
Transitivity follows chains. If A requires B, B requires transitive C, and C requires transitive D, then A reads B, C and D. A plain requires stops the chain, so a module that requires without transitive does not pass readability on to its own readers.
What readability does not give you
Reading a module does not make its types available by simple name. You still have to write import declarations or use fully qualified names. Readability only decides whether the compiler allows the reference at all.
Two more points are worth knowing:
- Exports are per package, not per type. Exporting
java.utilexposes every public type injava.util. There is no way to export one class from a package. - Subpackages are unrelated. Exporting
com.example.greeter.apidoes not exportcom.example.greeter.api.spi. Each package needs its ownexportsline.
Named and unnamed modules
Code in a module with a module-info.java is in a named module. It follows the rules above. Code compiled without a module-info.java, such as a single file or a classpath project, belongs to the unnamed module. Its rules differ in three ways:
- The unnamed module reads every module in the runtime's default root set. For ordinary applications this includes
java.base,java.sqland most other standard modules. - It exports all of its packages, but a named module cannot
requiresit. Named modules cannot read the unnamed module. - Because it reads so much, beginners often use JDK modules for years without writing a single
requires.
This difference matters later in this tutorial. Whether a module import is allowed depends on what the current module reads. Unnamed code can reach many modules by default, while a named module can only reach what its module-info.java declares.
Connecting this to import declarations
Everything above answers the question "which packages could this code possibly use?" The package import declarations you know, such as import java.util.*;, then decide which names appear without qualification. Until now these were two separate mechanisms. The module system said what you could reach, and imports said how you spelled it.
The import module declaration, which the next chapters cover, joins them. It names a module and imports the packages that module exports. For this to work, the module must be readable from your code, and only exported packages count. The rules you just reviewed (exports, requires, requires transitive, and named versus unnamed modules) decide what that one line can reach.
Check your understanding
Exercise
Module shop.core declares requires transitive shop.model;. Module shop.web declares only requires shop.core;. Can code in shop.web use a public type from an exported package of shop.model? What changes if shop.core is changed to a plain requires shop.model;?
Show answer
Yes, in the first case. shop.web reads shop.core, and requires transitive makes it read shop.model as well. The type is usable if its package is exported and the type is public. With a plain requires shop.model;, readability stops at shop.core. shop.web no longer reads shop.model, so the compiler reports that the package "does not read it" unless shop.web adds its own requires shop.model;.
Exercise
A public class Helper sits in package lib.internal of module lib. The module-info.java of lib contains exports lib.api; and no other exports. Module app has requires lib; and an import of lib.internal.Helper. Name the check that fails and give two ways to resolve it.
Show answer
The export check fails, and the compiler says the package lib.internal is declared in module lib, which does not export it. One fix is to add exports lib.internal; (or a qualified exports lib.internal to app;) if the class is meant to be shared. The other is to stop using Helper and use a type from lib.api instead.