CKTIS / Java
exports — module package export
exports — module package export: An exports directive in module-info.java gives other modules access to a package’s public top-level types. Public or protected members and nested types remain subject to the ordinary access-control rules.
Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime
Syntax
exportsAn exports directive in module-info.java gives other modules access to a package’s public top-level types. Public or protected members and nested types remain subject to the ordinary access-control rules.
Use exports p; for an unqualified export, or exports p to m1, m2; for a qualified export limited to named modules.
exports does not expose non-public members and does not replace opens for deep reflection; exporting an internal package expands the supported API surface.
Only the module containing a package can export it; a target module must still read the exporter through requires or another readability edge.
The descriptor shows an unqualified demo.api export to all modules and a qualified demo.internal export only to [friend.mod].
Examples
import java.lang.module.ModuleDescriptor;
import java.util.Comparator;
import java.util.Set;
public class Main {
public static void main(String[] args) {
var descriptor = ModuleDescriptor.newModule("demo.app")
.exports("demo.api")
.exports("demo.internal", Set.of("friend.mod"))
.build();
descriptor.exports().stream()
.sorted(Comparator.comparing(ModuleDescriptor.Exports::source))
.forEach(export -> System.out.println(export.source() + ":" +
(export.isQualified() ? export.targets() : "all")));
}
}
Result
demo.api:all demo.internal:[friend.mod]
The descriptor shows an unqualified demo.api export to all modules and a qualified demo.internal export only to [friend.mod].