Importance of Module Descriptor in a Module in Java 9?


A module is a collection of code in the form of classes organized in packages and static resources such as property files or others. It provides the outside environment with all the information that can be required to use that module. The module descriptor is a key source of the module system, and it's compiled version of a module declaration specified in a file named "module-info.java" file at the root of the module’s directory hierarchy.

The module describes itself by a module declaration as below

module com.myproject.module1 {
   requires com.myproject.module2;
   exports com.myproject.project1;
   exports com.myproject.project2;
}

Below are some of the module descriptors described:

  • module module. name: declares a module called module.name.
  • requires module. name: specifies that our module depends on the module. name, allows this module to access public types exported in the target module.
  • requires a transitive module. name: Any modules that depend on this module automatically depend on module.name.
  • exports pkg.name: It says that our module exports public members in package pkg.name for every module requiring this one.
  • exports pkg.name to module.name: It is the same as above, but limits which modules can use the public members from the package pkg.name.
  • uses class. name: It makes the current module a consumer for service class.name.
  • provides class.name with class.name.impl: It registers class.name.impl class a service that provides an implementation of the class.name service.
  • opens pkg.name: It allows other modules to use reflection to access the private members of package pkg.name.
  • opens pkg.name to module.name: It does the same, but limits which modules can have reflection access to the private members in the pkg.name.

Updated on: 26-Mar-2020

587 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements