Sub topic 4
Exporting packages as the module's API
This chapter assumes you can write a basic module-info.java with requires, and that you compile and run modules with javac and java on the module path. Here you will learn how exports decides which packages form a module's public API, what it does and doesn't expose, and which mistakes cause errors.
What exports does
A module is a set of packages. By default, none of them are visible to other modules. The exports directive makes one package accessible to every module that reads yours.
// src/com.example.greeter/module-info.java
module com.example.greeter {
exports com.example.greeter.api;
}Other modules can now use the public types in com.example.greeter.api. Any package you don't export stays internal to the module. Even a public class in an internal package cannot be used from outside.
Three rules cover most of what you need to know:
exportsnames a package, not a class and not a module.- It is not recursive. Exporting
com.example.greeterdoes not exportcom.example.greeter.api. - It only matters to other modules. Code inside the module sees all of its own packages.
An exported and a hidden package
This module has one exported package and one hidden 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;
import com.example.greeter.internal.Formatter;
public class Greeter {
public String greet(String name) {
return Formatter.decorate("Hello, " + name);
}
}
// src/com.example.greeter/com/example/greeter/internal/Formatter.java
package com.example.greeter.internal;
public class Formatter {
public static String decorate(String text) {
return "[" + text + "]";
}
}Formatter is public, so Greeter can use it across packages. A consumer module has no way to reach it:
// 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.internal.Formatter;
public class Main {
public static void main(String[] args) {
System.out.println(Formatter.decorate("hi"));
}
}Compiling this fails:
javac -d out --module-source-path src -m com.example.appMain.java:3: error: package com.example.greeter.internal is not visible
import com.example.greeter.internal.Formatter;
^
(package com.example.greeter.internal is declared in module com.example.greeter, which does not export it)Read the parenthesized line first. It names the module and states the cause, which tells you whether the fix belongs in the library or in your own code.
What an export exposes
Exporting a package gives access to its public and protected members, following the usual rules for those modifiers:
- Public types and their public members are accessible to any module that reads the exporting module.
- Protected members are accessible to subclasses outside the module.
- Package-private and private members are never accessible from outside, as before.
| Declaration | Visible to other modules? |
|---|---|
| `public` class in an exported package | Yes |
| `public` class in a non-exported package | No |
| Package-private class in an exported package | No |
| `protected` member of a public class in an exported package | Only to subclasses |
The module descriptor and the access modifiers work together. The descriptor decides which packages are reachable, and the modifiers decide which members inside them are usable.
The module graph view
An export only takes effect when another module reads yours. Two conditions must both hold: the consumer has requires for your module, and your module has exports for the package.
If you export a package but the consumer lacks the requires, the package is still not visible to that consumer. Compilation fails with a message that the module is not in the module graph or not read.
Exports and the API you promise
Treat the set of exported packages as a contract. Once other teams depend on com.example.greeter.api, moving or renaming it breaks them. The internal packages can change freely.
A practical layout is:
apipackages for types consumers use directly: interfaces, public classes, exceptions.internalorimplpackages for everything else.- Only the first group appears in
exports.
Leaking internal types through the API
The compiler lets you write an exported method that mentions a non-exported type:
package com.example.greeter.api;
import com.example.greeter.internal.Formatter;
public class Leaky {
public Formatter formatter() { // Formatter is not exported
return new Formatter();
}
}This compiles, though javac -Xlint:exports warns about it. A consumer can call formatter(), but it cannot name the return type, import it, or call its methods. Code that tries gets an IllegalAccessError at run time or a compile error. Avoid this by returning an exported interface instead.
Common errors
- Package does not exist.
exports com.example.greeter.api;with no classes in that package fails with "package is empty or does not exist". Check the directory path andpackagestatements. - Duplicate export. Listing the same package twice in one descriptor is a compile error.
- Exporting the wrong package. Exporting the parent package when the classes are in a sub-package. Remember rule 2: exports are not recursive.
- Missing requires on the consumer. The export is correct, but the consumer's descriptor doesn't name the module.
- Using a type from an internal package after upgrading a library. Code that compiled against a library's internals on the classpath fails once that library becomes a named module. The error text names the module that does not export the package.
Checking what a module exports
The java launcher can print a module's descriptor, including its exports, without any code:
java --module-path out --describe-module com.example.greeterThe output lists the module, what it requires, and its exported packages. For JDK modules the same command works, for example java --describe-module java.sql. Use it to confirm that a built jar exports what you intended.
Exercise
Module shop contains the packages com.shop.model and com.shop.model.internal. Its descriptor says exports com.shop.model;. Can a module that requires shop import a public class from com.shop.model.internal? Explain.
Show answer
No. exports is per package and not recursive. Only com.shop.model is exported. com.shop.model.internal is a different package, so it stays hidden even though its class is public. The compiler reports that the package is not visible because module shop does not export it. To allow it, add a separate exports com.shop.model.internal;, though keeping it hidden is usually the intent.
Exercise
An exported method in com.example.greeter.api returns a type from the non-exported package com.example.greeter.internal. Does the module compile, and what can consumers do with the result? How would you fix the design?
Show answer
The module compiles, with a warning if -Xlint:exports is on. Consumers can call the method but cannot name or import the returned type, so they can't use its members. The fix is to return an exported interface or class from the api package, and keep the internal implementation behind it.