Unnamed Module in Java
The unnamed module is a concept within the Java Platform Module System (JPMS) that plays a crucial role in maintaining backward compatibility with older Java applications. It serves as a bridge between modular and non-modular code, allowing legacy applications to run on newer Java runtimes without requiring them to be refactored into modules. This feature is particularly useful for applications developed with Java 8 or earlier, which do not utilize the module system introduced in Java 9.
Overview
The unnamed module encompasses all classes and JAR files present on the classpath. This means that any non-modular Java application, when executed, will have its classpath contents automatically included in the unnamed module. It is a special module that has no explicit name, reads all other modules in the module graph, and exports all its packages. This allows it to integrate class-path content into the module system seamlessly.
For more details, refer to the Unnamed Module in Java.
Characteristics of the Unnamed Module
-
Class Path Content: The unnamed module captures all class-path content, which includes all non-modular classes. This is particularly important when migrating an application to the module system, as it allows existing class-path content to be part of the module system without requiring immediate modularization.
-
Backward Compatibility: The primary purpose of the unnamed module is to provide backward compatibility. It allows older Java applications to run on newer Java versions without modification.
-
Module Graph Integration: The unnamed module can read all other modules in the module graph. This means it has access to all the modules that are part of the application, ensuring that non-modular classes can interact with modular ones.
-
Exports and Reflection: The unnamed module exports all its packages and opens them for reflection. This feature is essential for maintaining backward compatibility with older Java applications that rely on reflection.
-
Dependency: The unnamed module cannot be explicitly depended upon by modular code. This means that while modular code can access the classes in the unnamed module, it cannot declare a dependency on it in a
module-info.javafile. -
Dependencies: The unnamed module implicitly depends on all modules present in the Java Development Kit (JDK) and those on the module path. This ensures that it has access to all necessary APIs and libraries provided by the JDK.
For more insights, see Characteristics of the Unnamed Module.
Module Resolution
Module resolution for the unnamed module begins with a predefined set of root modules, typically system modules that do not contain Java EE APIs. While the unnamed module can read all modules that are part of the graph, modules on the module path are not resolved unless they are explicitly added using the --add-modules option.
For further reading, visit Module Resolution.
Example Usage
An example of the unnamed module in action is when launching an application from the class path. Consider the following command:
$ java --class-path 'jars/*':'libs/*' monitor.Main
In this scenario, all classes on the class path are part of the unnamed module, which can read all other modules. This setup is compatible with Java 9 and later versions, and it behaves similarly to Java 8 and earlier, aside from the alternative form of the --class-path option.
For more examples, see Example Usage.
Visual Representation
The following figure illustrates how the module system builds a module graph when an application is launched with all JARs on the class path. The platform modules are on the left, and all classes on the class path are assigned to the unnamed module on the right, which can read all other modules.

Figure 8.2 Launched with all application JARs on the class path, the module system builds a module graph from the platform modules (left) and assigns all classes on the class path to the unnamed module (right), which can read all other modules.
Understanding the unnamed module is essential for running simple, non-modular applications from the class path and is particularly relevant when gradually modularizing an application.
| Book Title | Usage of unnamed 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 the unnamed module as a special module for non-modular classes, crucial for backward compatibility and incremental modularization. [more](https://livebook.manning.com/book/the-java-9-module-system/chapter-8/41) | Explains module graph integration, exports, and reflection, providing a deep dive into the unnamed module's characteristics. [more](https://livebook.manning.com/book/the-java-9-module-system/chapter-8/62) | Connects the unnamed module to module resolution and the module graph, highlighting its role in the Java module system. [more](https://livebook.manning.com/book/the-java-9-module-system/chapter-8/75) | Provides a command-line example illustrating the unnamed module's use in launching applications. [more](https://livebook.manning.com/book/the-java-9-module-system/chapter-8/1) | Emphasizes the unnamed module's role in running non-modular applications and gradual modularization. [more](https://livebook.manning.com/book/the-java-9-module-system/chapter-8/59) |
| [The Well-Grounded Java Developer, Second Edition](https://www.manning.com/books/the-well-grounded-java-developer-second-edition) | Describes the unnamed module as a bridge for backward compatibility, allowing legacy applications to run on newer Java runtimes. [more](https://livebook.manning.com/book/the-well-grounded-java-developer-second-edition/chapter-2/186) | Focuses on backward compatibility and the unnamed module's implicit dependencies on JDK modules. [more](https://livebook.manning.com/book/the-well-grounded-java-developer-second-edition/chapter-2/186) | Highlights the unnamed module's role in maintaining compatibility with non-modular code and its implicit dependencies. [more](https://livebook.manning.com/book/the-well-grounded-java-developer-second-edition/chapter-2/186) | Discusses running non-modular applications on modern Java runtimes, using Java 8 applications as examples. [more](https://livebook.manning.com/book/the-well-grounded-java-developer-second-edition/chapter-2/149) | Focuses on preserving existing Java software investments while leveraging new Java features. [more](https://livebook.manning.com/book/the-well-grounded-java-developer-second-edition/chapter-2/186) |
FAQ (Frequently asked questions)
What is the unnamed module in Java?
Can modular code depend on the unnamed module?
What does the unnamed module depend on?
How are nonmodular apps handled in Java 11?
What is the root module for nonmodular apps in Java 11?
What are unnamed modules in Java?
What does the unnamed module in Java include?
Why is the unnamed module important for backward compatibility?
How does the module system handle classes on the class path?
What happens when an application is launched from the class path?
How does the unnamed module handle class-path content?
What does the unnamed module export?
How does module resolution work for the unnamed module?
Are modules on the module path resolved by default for the unnamed module?