Automatic Module
Automatic modules are a feature of the Java Platform Module System (JPMS) introduced to facilitate the transition from non-modular to modular Java applications. They allow plain JAR files to be treated as modules without requiring a module descriptor, thus enabling existing libraries to be used in a modular environment without immediate full modularization.
Overview
Automatic modules are created when a nonmodular JAR file is placed on the module path. This process automatically converts the JAR into a module, which exports all its packages and can read all other modules. This feature acts as a bridge, allowing developers to integrate existing libraries into modular applications without waiting for those libraries to be fully modularized. However, automatic modules do not have explicit module dependency information and are not considered first-class citizens within the module system.
For example, the Apache Commons Lang library, although not fully modularized, provides an automatic module name, org.apache.commons.lang3. This allows other modules to declare dependencies on it, facilitating its use in a modular application.
Module Names
The name of an automatic module can be specified in the JAR's manifest using the Automatic-Module-Name header. If this header is absent, the module system infers a name from the filename of the JAR, which can sometimes lead to inconsistencies.
Module Resolution
Automatic modules are resolved as soon as the first one is encountered. They do not express explicit dependencies, meaning they do not automatically pull in platform modules. Instead, all automatic modules are resolved together, which can lead to cycles in the module graph. This requires careful management of dependencies to avoid issues.
Cycles in Module Graphs
Automatic modules inherently read all other modules, which can introduce cycles in the module graph. These cycles are dynamically introduced by the module system and do not violate the rule against static dependency cycles.
Choosing Paths for Transitive Dependencies
When dealing with automatic modules' dependencies, you generally have two options: the class path or the module path. Plain JARs are included through an all-or-nothing approach to automatic module loading. Platform and explicit application modules are not resolved by default and need to be added manually using the --add-modules option.
Figures
[Figure 8.6](https://livebook.manning.com/the-java-9-module-system/chapter-8/figure--8-6)](https://livebook.manning.com/book/the-java-9-module-system/chapter-8/115)

With the plain JARs monitor.jar, monitor.observer.jar, and monitor.statistics.jar on the module path, the JPMS creates three automatic modules for them. The class-path content ends up in the unnamed module as before. Note how automatic modules read each other and the unnamed module, creating lots of cycles in the graph.
[Figure 8.8](https://livebook.manning.com/the-java-9-module-system/chapter-8/figure--8-8)](https://livebook.manning.com/book/the-java-9-module-system/chapter-8/115)

If a project (your.app in this case) uses an automatic module (org.jooq), you can’t be sure the module graph works out of the box. Automatic modules don’t express dependencies, so platform modules they need may not make it into the graph (here, that happened with java.sql) and have to be added manually with --add-modules.
[Figure 8.9](https://livebook.manning.com/the-java-9-module-system/chapter-8/figure--8-9)](https://livebook.manning.com/book/the-java-9-module-system/chapter-8/115)

Starting with the dependency of monitor.rest (a modular JAR) on spark.core (a plain JAR), the latter needs to be placed on the module path. But what about its dependency slf4j (another plain JAR)? Here you see the resulting module graphs depending on whether slf4j is required by another modular JAR (top versus bottom row) or which path it’s placed on (middle versus right column). Looks like a clear win for the module path, but take a look at figure 8.10.
[Figure 8.10](https://livebook.manning.com/the-java-9-module-system/chapter-8/figure--8-10)](https://livebook.manning.com/book/the-java-9-module-system/chapter-8/178)

In the same situation as figure 8.9's bottom right corner, what happens if an automatic module’s transitive dependency (slf4j) that’s placed on the module path gets modularized? It’s no longer resolved by default and needs to be added manually with --add-modules.
[Figure 8.11](https://livebook.manning.com/the-java-9-module-system/chapter-8/figure--8-11)](https://livebook.manning.com/book/the-java-9-module-system/chapter-8/115)

If a method in an automatic module (org.lib in this case) returns a type from the unnamed module (ImmutableList), named modules (your.app) can’t access it, because they don’t read the unnamed module. This crashes the application if the method declares that it returns the inaccessible type (ImmutableList). Declaring a supertype (here, most likely List) would work.
| Book Title | Usage of Automatic Module | Technical Depth | Connections to Other Concepts | Examples Used | Practical Application |
|------------|---------------------------|-----------------|-------------------------------|---------------|-----------------------|
| [The Java Module System](https://www.manning.com/books/the-java-9-module-system) | Discusses automatic modules as a feature of JPMS that allows plain JAR files to be treated as modules without a module descriptor. [more](https://livebook.manning.com/book/the-java-9-module-system/chapter-8/164) | Explores module names, resolution, and cycles in module graphs. [more](https://livebook.manning.com/book/the-java-9-module-system/chapter-8/161) | Connects automatic modules to module resolution and dependency management. [more](https://livebook.manning.com/book/the-java-9-module-system/chapter-8/178) | Uses figures to illustrate module graphs and dependency management. [more](https://livebook.manning.com/book/the-java-9-module-system/chapter-8/115) | Provides guidance on managing dependencies and choosing paths for transitive dependencies. [more](https://livebook.manning.com/book/the-java-9-module-system/chapter-8/178) |
| [The Well-Grounded Java Developer, Second Edition](https://www.manning.com/books/the-well-grounded-java-developer-second-edition) | Describes automatic modules as a bridge for nonmodular JARs to be used in a modular environment. [more](https://livebook.manning.com/book/the-well-grounded-java-developer-second-edition/chapter-2/180) | Briefly covers the concept without deep technical details. [more](https://livebook.manning.com/book/the-well-grounded-java-developer-second-edition/chapter-2/180) | Highlights the role of automatic modules in integrating existing libraries into modular applications. [more](https://livebook.manning.com/book/the-well-grounded-java-developer-second-edition/chapter-2/180) | Provides an example with Apache Commons Lang library. [more](https://livebook.manning.com/book/the-well-grounded-java-developer-second-edition/chapter-2/149) | Focuses on practical integration of libraries using automatic module names. [more](https://livebook.manning.com/book/the-well-grounded-java-developer-second-edition/chapter-2/149) |
FAQ (Frequently asked questions)
What are automatic modules in Java?
Do automatic modules have proper module dependency information?
How does the Apache Commons Lang library use automatic modules?
What are automatic modules in Java?
Can automatic modules be referenced by name?
What happens when plain JARs are placed on the module path?
Do automatic modules create cycles in the module graph?
How can automatic module names be defined?
What happens if the Automatic-Module-Name header is absent?
When are automatic modules resolved?
Do automatic modules express dependencies?
Can automatic modules lead to cycles in the module graph?
Do automatic modules express explicit dependencies?
What happens when the first automatic module is resolved?
Do automatic modules create cycles in module graphs?
Are cycles in module graphs a violation of static dependency rules?
What are the options for automatic modules' dependencies?
How are plain JARs pulled in for automatic modules?
Are platform and explicit application modules resolved by default?