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

Requiring Automatic Modules

How explicit modules can require automatic modules by name, creating a dependency on legacy JARs.

Requiring Automatic Modules

What you'll learn: How to explicitly depend on automatic modules from your own modular code, bridging the gap between JPMS and legacy JARs.

Creating Dependencies on Automatic Modules

Once a legacy JAR becomes an automatic module (by being placed on the module path), your explicit modules can declare dependencies on it using the requires directive—just like with any other module.

You reference the automatic module by the name derived from either its Automatic-Module-Name manifest entry or its JAR filename (as you learned in previous lessons). This creates a formal, declarative dependency on what was originally non-modular code.

Syntax and Usage

In your module-info.java, you simply write:

module my.app {
    requires commons.lang3;  // assuming commons-lang3 is an automatic module
}

This tells the module system that my.app needs access to all packages exported by the commons.lang3 automatic module. Remember, automatic modules export all their packages, so you gain access to everything in that JAR.

Why This Matters

This mechanism is your migration bridge. You can start modularizing your own code (creating explicit modules with module-info.java) while still depending on third-party libraries that haven't been fully modularized yet. The automatic module acts as a "compatibility shim" between the old and new worlds.

Important Considerations

When you requires an automatic module, you're depending on its automatically-derived name—which might change if the JAR file is renamed or updated. Always prefer JARs with an explicit Automatic-Module-Name manifest entry for stability.

Key Takeaway: Explicit modules can requires automatic modules by their derived name, allowing you to incrementally modularize applications while maintaining dependencies on legacy JARs.