Is in unnamed module of loader ‘app’

Query: Is in unnamed module of loader ‘app’

When you encounter the error message “is in unnamed module of loader ‘app'” in your application, it means that the specified module or resource was not found in the classpath or module path at runtime by the Java Virtual Machine (JVM) during execution.

To fix this error, you need to understand the concepts of Java modules and how they relate to the classpath. Starting from Java 9, Java introduced a module system (Project Jigsaw) to bring modularity to the JDK and application development.

A module is a self-contained unit of code that encapsulates its implementation details and exposes a set of public APIs. It helps in organizing and modularizing large code bases, enforcing strong encapsulation, and managing dependencies between different modules.

When a module is compiled, it can explicitly declare its dependencies on other modules using the requires keyword in the module-info.java file. The JVM then uses this information to resolve and load the required modules at runtime.

However, if a required module is missing from the module path or classpath, you will encounter the error “is in unnamed module of loader ‘app'”. This error typically occurs when you have a mix of modules and non-modular code (legacy JARs) in your application, and the JVM cannot resolve the dependencies correctly.

Example:

Let’s consider an example where you have a modular application with two modules: com.example.app and com.example.utils.


    - app
      - src
        - com.example.app
          - module-info.java
          - com.example.app.Main.java
      - module-info.java
    - utils
      - src
        - com.example.utils
          - module-info.java
          - com.example.utils.Utility.java
      - module-info.java
  

In the module-info.java file of the com.example.app module, you would have a requires directive to specify that it depends on the com.example.utils module. Similarly, in the module-info.java file of the com.example.utils module, you might have some exports and/or opens directives.


    // module-info.java of com.example.app
    module com.example.app {
      requires com.example.utils;
    }
    
    // module-info.java of com.example.utils
    module com.example.utils {
      exports com.example.utils;
    }
  

Now, when you compile and run your application, you need to make sure that all the required modules are present in the module path or classpath.


    // Compile and run with modules
    javac -d app/bin app/src/com.example.app/module-info.java app/src/com.example.app/com.example.app.Main.java
    javac -d utils/bin utils/src/com.example.utils/module-info.java utils/src/com.example.utils/com.example.utils.Utility.java
    java --module-path app/bin:utils/bin --module com.example.app/com.example.app.Main
  

If the required modules (in this case, com.example.utils) are not found in the module path or classpath, you will encounter the error “is in unnamed module of loader ‘app'”.

Additional Notes:

  • Make sure you have correctly declared the module dependencies and exports/opens directives in the module-info.java files.
  • Check if the required modules are present in the module path or classpath.
  • Ensure that you are using the correct module names in the requires directives.
  • If you are using frameworks or libraries that are not modularized, you may need to add them to the classpath instead of the module path.

Same cateogry post