What is an unnamed module in Java 9?

JavaObject Oriented ProgrammingProgramming

An unnamed module is a concept of the unnamed package. It is a module in which packages or classes can't be defined in any named module but exist in the jar file from classpath. If our code can try to load type from those files, the module system attempts to lookup classpath and loads it.

An unnamed module read all other modules, including all of the named, built-in platform modules, and also exports all of its packages. The package in an unnamed module can be ignored, which is also defined in the named module.

The unnamed module has access to:

  • All packages exported by all other modules available in module-path.
  • All the jars of the classpath (i.e. all the other types present in this unnamed module).

Syntax

java --module-path out -module moduleName/com.tutorialspoint.UnnamedModuleTest

Example

public class UnnamedModuleTest {
   public static void main(String args[]) {
      Module module = UnnamedModuleTest.class.getModule();
      System.out.println("Module: "+ module);
      System.out.println("Name: " + module.getName());
      System.out.println("isNamed: " + module.isNamed());
      System.out.println("Descriptor: " + module.getDescriptor());
   }
}

Output

Module: unnamed module @c818063
Name: null
isNamed: false
Descriptor: null
raja
Published on 08-Apr-2020 11:52:20
Advertisements