Sub topic 10
Qualified exports
Assume you know Java packages, module-info.java, and the difference between compiling on the classpath and module path. This chapter focuses on controlling which named modules can access an exported package.
A normal export makes a package available to every module that reads the exporting module:
module com.example.library {
exports com.example.library.api;
}A qualified export adds a to clause. Only the listed modules can access the exported package:
module com.example.library {
exports com.example.library.api;
exports com.example.library.internal
to com.example.library.tests;
}Here, com.example.library.internal is visible to com.example.library.tests, but not to other named modules. The classes in that package must still be public for another module to use them.
A library and its test module
Suppose the library has an internal helper that its own test module must inspect:
// src/com.example.library/module-info.java
module com.example.library {
exports com.example.library.api;
exports com.example.library.internal
to com.example.library.tests;
}
// src/com.example.library/com/example/library/internal/ParserSupport.java
package com.example.library.internal;
public class ParserSupport {
public static String normalize(String value) {
return value.trim().toLowerCase();
}
}The test module names the library as a dependency:
// src/com.example.library.tests/module-info.java
module com.example.library.tests {
requires com.example.library;
}
// src/com.example.library.tests/com/example/library/tests/InternalTest.java
package com.example.library.tests;
import com.example.library.internal.ParserSupport;
public class InternalTest {
public static void main(String[] args) {
System.out.println(ParserSupport.normalize(" Hello "));
}
}Compile and run both modules from the source tree:
javac -d out --module-source-path src \
-m com.example.library,com.example.library.tests
java --module-path out \
-m com.example.library.tests/com.example.library.tests.InternalTestThe compiler allows the test module to import ParserSupport because the library has a qualified export to that exact module.
Common errors and boundaries
| Situation | Result |
|---|---|
| A listed module requires the exporting module | The qualified package is accessible |
| An unlisted named module imports the package | Compilation fails |
| Code on the classpath imports the package | It cannot use the qualified export |
| A public class is missing from the package export | It remains inaccessible |
A qualified export is still an API commitment. Use it for narrow integration points, test support, or carefully controlled extensions. If a framework needs deep reflection rather than ordinary access, use opens package to module instead. exports controls compilation and normal access; opens controls reflective access at run time.
Exercise
A library exports com.example.library.internal to com.example.library.tests. Can com.example.app import a public class from that package if it requires the library?
Show answer
No. The package is qualified for the test module only. The library must add com.example.app to the qualified export or expose a different public package.