Exporting Multiple Packages from One Module
You'll learn how to make several packages publicly accessible from a single module by adding multiple exports directives to your module descriptor.
The Core Idea
Just as a library might have multiple sections available to the public, a module can expose several different packages to other modules. You simply list as many exports directives as needed—one per package you want to make visible.
Each exports statement is independent and makes exactly one package accessible. There's no shortcut syntax to export multiple packages in a single line; you must declare each separately.
Why Multiple Exports Matter
Real-world modules often organize functionality into logical packages. For example, a graphics module might have separate packages for shapes, colors, and rendering utilities. You might want consumers to access all three, but keep internal implementation packages hidden.
Syntax in module-info.java
module my.graphics {
exports com.graphics.shapes;
exports com.graphics.colors;
exports com.graphics.rendering;
// Internal package NOT exported:
// com.graphics.internal remains hidden
}
Each exports directive stands alone. The order doesn't matter, but listing them clearly shows exactly what your module's public API surface includes.
Combining with Qualified Exports
You can mix unrestricted and qualified exports freely:
module my.data {
exports com.data.api; // Public to all
exports com.data.spi to my.plugin; // Only to my.plugin
exports com.data.utils; // Public to all
}
This gives you fine-grained control: some packages fully public, others restricted to specific consumers, and the rest completely encapsulated.
Key Takeaway:
Export as many packages as needed by listing separate exports directives—one per package—giving you complete control over which parts of your module form its public API.