Sub topic 10
Qualified exports
What you need before reading
This chapter assumes you can write a basic module-info.java with requires and exports. It also assumes you know what the module path is and how javac compiles several modules together. If exports still feels unfamiliar, review the chapter on exporting packages first.
The problem: exports are all or nothing
A plain exports directive makes a package's public types accessible to every module that can read your module. Sometimes that is too wide.
Suppose a library has an internal package. Its own modules and its test module need it, but you do not want to promise it to the world. Without qualified exports you have two bad choices:
- Export the package to everyone. It becomes part of your public API, and users will depend on it.
- Keep the package unexported. Your sibling modules then cannot use it either.
A qualified export is the middle path. It exports a package only to the modules you name.
Syntax
Add to and a comma-separated list of module names:
module com.example.library {
exports com.example.library.api;
exports com.example.library.internal to com.example.library.tests;
}Here com.example.library.api is open to all readers. com.example.library.internal is accessible only to com.example.library.tests. You can name several modules:
module com.example.library {
exports com.example.library.internal
to com.example.library.tests, com.example.library.tools;
}Inside the named modules, the exported types behave as if the package were exported normally. This applies to public types and to protected members that code in a subclass can reach. Everyone else gets the same compile error and runtime IllegalAccessError they would get for a package that is not exported at all.
A worked example
Build a three-module layout. The core module has a public API and a package shared with a sibling module.
// src/com.example.core/module-info.java
module com.example.core {
exports com.example.core.api;
exports com.example.core.spi.support to com.example.plugin;
}
// src/com.example.core/com/example/core/api/Core.java
package com.example.core.api;
public class Core {
public String name() {
return "core";
}
}
// src/com.example.core/com/example/core/spi/support/Helper.java
package com.example.core.spi.support;
public class Helper {
public static String decorate(String s) {
return "[" + s + "]";
}
}The plugin module is allowed to use the helper:
// src/com.example.plugin/module-info.java
module com.example.plugin {
requires com.example.core;
}
// src/com.example.plugin/com/example/plugin/Plugin.java
package com.example.plugin;
import com.example.core.api.Core;
import com.example.core.spi.support.Helper;
public class Plugin {
public static void main(String[] args) {
System.out.println(Helper.decorate(new Core().name()));
}
}An app module that also requires core is not on the list:
// src/com.example.app/module-info.java
module com.example.app {
requires com.example.core;
}
// src/com.example.app/com/example/app/Main.java
package com.example.app;
import com.example.core.spi.support.Helper; // compile error
public class Main {
public static void main(String[] args) {
System.out.println(Helper.decorate("x"));
}
}Compile everything and run the plugin:
javac -d out --module-source-path src -m com.example.plugin
java --module-path out -m com.example.plugin/com.example.plugin.PluginThis prints [core]. Compiling com.example.app fails with a message like package com.example.core.spi.support is declared in module com.example.core, which does not export it to module com.example.app. That wording tells you the package exists and the export is qualified.
The next diagram shows which module may read which package.
Common uses
Qualified exports fit a few recurring situations:
- Test modules. A test module in the same build needs access to internals that production users must not see.
- Split by function. You divide a product into several modules, such as
core,server, andcli. The modules share an internal package, but outsiders see only the public API. - Staged APIs. You let a few trusted modules use an experimental package before you commit to making it public.
The JDK uses this heavily. For example, java.base exports several jdk.internal packages only to chosen JDK modules, which keeps them away from application code. You can see this with:
java --describe-module java.baseLook for lines that begin with qualified exports.
Edge cases and gotchas
The target module does not have to exist
The compiler checks whether the target module is present. If you name a module that is not in the module graph, javac reports a warning, not an error: module not found: com.example.tests. The reason is that the target might be present at run time but not at compile time. Treat this warning as a sign of a typo unless you know the module is deliberately absent. A misspelled name will silently make the export useless.
A qualified export does not create a dependency
Naming a module in to does not make that module read yours. The target still needs its own requires com.example.library. The export controls accessibility. The requires controls readability. Both are needed. In the worked example, plugin would fail without requires com.example.core, even though the export names it.
It is not a security boundary against reflection tricks
A qualified export limits compile-time and normal run-time access. It is not a sandbox. Reflection is governed by opens, which has its own qualified form, and command-line flags such as --add-exports can widen access at launch. Treat qualified exports as a design tool for API boundaries, not as protection against hostile code.
You can add an export from the command line for code that you cannot change:
java --add-exports com.example.core/com.example.core.spi.support=com.example.app \
--module-path out -m com.example.app/com.example.app.MainThe format is module/package=target. Use ALL-UNNAMED as the target to grant access to classpath code. Use this flag as a short-term workaround, not a permanent design.
Qualified exports and split packages
A package can be exported either to everyone or to specific modules, but one module cannot export the same package both ways. Duplicate exports for the same package is a compile error. To widen access, replace the qualified directive with a plain one.
Qualified `opens` follow the same pattern
The opens directive accepts the same to clause:
module com.example.library {
opens com.example.library.model to com.fasterxml.jackson.databind;
}This permits deep reflection on the package only from the named module. Prefer this over a plain opens when you know which framework needs the access.
Choosing between plain and qualified exports
| Situation | Choice |
|---|---|
| The package is your supported public API | Plain `exports` |
| Only sibling modules you control need it | Qualified `exports ... to` |
| A framework needs reflection on a model package | Qualified `opens ... to` |
| Unknown or many future consumers need it | Plain `exports`, or redesign the API |
| You need a temporary workaround for a third-party module | `--add-exports` on the command line |
A useful test is whether you can list the consumers by name. If you can, and the list is short and stable, a qualified export documents that fact. If the list keeps growing, the package is probably part of your API and should be exported plainly.
Summary
Qualified exports narrow a package's audience to named modules. They keep internals available to trusted siblings while hiding them from everyone else. The target modules still need requires, a missing target module produces only a warning, and the same package cannot be exported both ways.
Exercise
A module com.shop.orders contains the package com.shop.orders.internal. The module com.shop.reports should use that package, but com.shop.web should not. Write the exports line in com.shop.orders, and name the one other directive that com.shop.reports must declare for this to compile.
Show answer
In com.shop.orders, write exports com.shop.orders.internal to com.shop.reports;. The com.shop.reports module must also declare requires com.shop.orders;, because the qualified export only grants accessibility. Readability still comes from requires.
Exercise
You compile a module that contains exports com.example.api to com.example.tets; (note the typo), and the build succeeds. Why does it succeed, and what is the consequence?
Show answer
The compiler reports only a warning ("module not found") for an unknown target, because the module might exist at run time. The consequence is that no real module receives the export, so the intended consumer, com.example.tests, gets an access error until the name is corrected.