Open In App

Java Startup Detection of Missing Modules in a Module with Examples

Last Updated : 05 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Java provides a module system to help developers create modular applications. A module is a self-contained unit of code that provides a specific functionality or feature. A module can depend on other modules and declare its own set of exported and internal packages. The module system provides many benefits such as improved maintainability, better security, and faster application startup times. However, if a module is missing a required module, it can lead to runtime errors and unexpected behavior. Therefore, it is essential to detect missing modules in a module during the application startup phase. This article will cover the concept of detecting missing modules in a module with examples. It will cover all dimensions of the concept, including theory, approaches, and practical examples.

Approaches

Approach 1: Using the command line

One approach to detecting missing modules in a module is to use the command line. When launching a modular application, we can pass the argument `–list-modules` to list all the modules and their dependencies. This command will also list any missing modules that the application requires. For example, consider the following module-info.java file:

Java




module com.example.mymodule {
    requires java.base;
    requires com.example.dependencymodule;
}


If the `com.example.dependencymodule`  module is missing during application startup, we will see the following error message:

Error occurred during initialization of boot layer
java.lang.module.FindException: Module com.example.mymodule requires module com.example.dependencymodule which is not found

Approach 2: Using the module system API

Another approach to detecting missing modules in a module is to use the module system API. The `ModuleLayer` class provides a method called `findModule` that can be used to check if a module is present in the application. If the module is missing, an `Optional.empty` value will be returned. For example, consider the following code snippet:

Java




ModuleLayer layer = ModuleLayer.boot();
Optional<Module> module = layer.findModule("com.example.dependencymodule");
if (module.isEmpty()) {
    System.out.println("The com.example.dependencymodule module is missing.");
}


If the `com.example.dependencymodule` module is missing, the message “The com.example.dependencymodule module is missing.” will be printed.

Examples

Using the command line:

Java




module com.example.mymodule {
    requires java.base;
    requires com.example.dependencymodule;
}
  
public class Main {
    public static void main(String[] args) {
        System.out.println("Application started.");
        // Check for missing modules
        // using the command line
        // argument --list-modules
    }
}


Output:

Error occurred during initialization of boot layer
java.lang.module.FindException: Module com.example.mymodule requires module com.example.dependencymodule which is not found

Using the module system API:

Java




module com.example.mymodule {
    requires java.base;
    requires com.example.dependencymodule;
}
  
public class Main {
    public static void main(String[] args) {
        System.out.println("Application started.");
        // Check for missing modules
        // using the module system API
        ModuleLayer layer = ModuleLayer.boot();
        Optional<Module> module = layer.findModule("com.example.dependencymodule");
        if (module.isEmpty()) {
            System.out.println("The com.example.dependencymodule module is missing.");
        }
    }
}


Output:

The com.example.dependencymodule module is missing.

Conclusion

Detecting missing modules in a module is essential to ensure that the application starts up correctly and avoids runtime errors. In this article, we covered the concept of detecting.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads