Open In App

Unnamed Module in Java

Java module is a group of closely related packages and resources along with a new module descriptor file. Also, the module is a collection of packages designed for reuse. A Java module can identify which of the Java packages it contains that should be visible to other Java modules which use this module.is a package in a modular JAR file with a module descriptor in its root folder or a mechanism to pack the java applications or API separately.

Types of Modules 

Type 1: Unnamed Module



An unnamed module is a JAR that is built without module-info.java declaration. An unnamed module will require all other modules and will export all its packages as well.

Type 2: Named Module



A named module is a module that is created with a module declaration file module-info.java in its root folder. The module has a name and it depends on some other modules. This means that when compiling and running this module, the required modules have to be present, otherwise compilation error or runtime error may occur.

Type 3: Automatic Module

An automatic module is a module that can read all other modules and all other modules can read it also the vice-versa. Even the named modules can read an automatic module.  An automatic module will have a module name that is derived from its jar name.

Here we will be stressing out only on the unnamed module as follows: 

Unnamed Module

An unnamed module is a jar construct without a module-info.java declaration. Since an unnamed module can render all other modules, this doesn’t require a current application to be modularized before using Java 9. One thing to note is an unnamed module that exports whole its packages doesn’t mean a named module can access unnamed module packages. This is because there is no way in module-info.java to declare requires unnamed module. An unnamed module will require all other modules and will export whole its packages as well. This gives a vast privilege to new applications to use Java 9 and Java 9 doesn’t allow named modules to read unnamed modules. This only ensures other unnamed modules can access its packages.

Now let us list some of the uses of the Unnamed Module pertaining to which it is introduced

Implementation:

We will be creating an unnamed module using the Module class that comes under java.lang in-built module and then finally print name and description of the module.

Example:




// Java Program to Get the Name of Unnamed Module
// and The Description of That Module
  
// Main class
public class GFG_UnnamedModule {
  
    // Main driver method
    public static void main(String args[])
    {
  
        // Getting the module using getModule() method
        Module module = UnnamedModuleTest.class.getModule();
  
        // Printing the name and description of the module
        System.out.print("Name of Module is: "
                         + module.getName());
        System.out.println("Description of module: "
                           + module.getDescriptor());
    }
}

Output:

Name of Module: is null
Description of module: null
Article Tags :