Course contentsShow
← Java
Lesson 2481 of 2,87047. Java Platform Module System (JPMS)Free lesson

Basic exports Syntax in module-info.java

Learn the syntax for exporting packages: exports com.example.api; makes all public types in that package available.

Basic exports Syntax in module-info.java

You'll learn the exact syntax for exporting a package so other modules can use its public types.

Now that you understand the exports directive controls package visibility, it's time to write one yourself. The syntax is beautifully simple and follows a clear pattern that makes your module's API explicit.

The Core Syntax

To export a package from your module, add this line inside your module-info.java:

exports com.example.api;

That's it. This single declaration makes all public classes and interfaces in the com.example.api package accessible to any module that requires your module.

Breaking It Down

  • exports — the keyword that declares visibility
  • com.example.api — the fully qualified package name (must exist in your module's directory structure)
  • ; — terminated with a semicolon, like all Java declarations

Complete Example

Here's a typical module-info.java with basic exports:

module com.example.library {
    exports com.example.api;
    exports com.example.util;
}

This module exposes two packages. Any other module that declares requires com.example.library; can now use public types from both com.example.api and com.example.util.

What Gets Exported?

Only public and protected types become accessible. Package-private and private types remain hidden, even in an exported package. Think of exports as opening a gate—but only VIP members (public types) can walk through.

What Stays Hidden?

Any package you don't export remains completely inaccessible to other modules, regardless of the public keyword. Strong encapsulation at work!

Key Takeaway: Use exports packageName; in module-info.java to make all public types in that package available to other modules—it's the foundation of your module's public API.