Java Automatic Modules

An automatic module is a JAR placed on the module path that has no `module-info.class`, so the Java module system derives a module descriptor for it. Its module name comes from the `Automatic-Module-Name` manifest entry when present, otherwise from the JAR filename.

Automatic modules provide a migration path for libraries that have not yet adopted JPMS module descriptors. When such a JAR is placed on the module path, the module system treats it as a named module even though the JAR itself does not contain `module-info.class`.

The module name is determined first from the JAR manifest's `Automatic-Module-Name` attribute. If that attribute is absent, the name is derived from the JAR filename using the automatic-module-name rules. This makes the manifest entry useful when a library wants a stable JPMS name that does not depend on its filename.

An automatic module exports all of its packages to other named modules, unlike a normal explicit module whose exported packages must be declared in `module-info.java`. It also has broad readability: an automatic module can read all other observable modules on the module path.

Automatic modules are mainly a compatibility mechanism, not a substitute for a carefully designed explicit module. They let modular applications depend on older libraries, while still allowing those libraries to be migrated to explicit module descriptors later.

Key points

Interview questions

How do automatic modules differ from explicit modules in terms of encapsulation?

An explicit module declares which packages it exports in its module descriptor. An automatic module instead exports all of its packages to other named modules. This makes automatic modules much less restrictive as a migration mechanism. Code should therefore not treat an automatic module as having the same strong encapsulation boundaries as a deliberately designed explicit module.

What is an automatic module in Java?

It is a JAR without a `module-info.class` that is placed on the module path and treated as a named module. The Java module system creates a module descriptor for it automatically. Its name comes from `Automatic-Module-Name` when supplied, otherwise from the JAR filename. It is primarily a compatibility mechanism for libraries that have not yet adopted JPMS explicitly.

How is the name of an automatic module determined?

The module system first checks the JAR manifest for `Automatic-Module-Name`. If it is present, that value defines the module name. If it is absent, the name is derived from the JAR filename according to the JPMS naming rules. Therefore, publishing a stable `Automatic-Module-Name` can prevent the module name from changing merely because an artifact filename changes.

Why were automatic modules introduced?

JPMS needed a practical way for modular applications to use existing libraries that had not yet added module descriptors. Automatic modules provide those libraries with named-module behavior without requiring their source code to be immediately converted. This supports incremental migration to JPMS. They are therefore best understood as an interoperability mechanism rather than the final module design.

What readability behavior should you remember for automatic modules?

An automatic module has an implied readability relationship to every other observable module in the module configuration. This gives legacy libraries broad access while they are being used as automatic modules. That behavior is intentionally permissive compared with the explicitly declared `requires` relationships of ordinary named modules. It is one reason automatic modules should not be viewed as equivalent to carefully encapsulated explicit modules.