How can we modify an existing module in Java 9?

The module is a named, self-describing collection of code and data. The code has been organized as a set of packages containing types like Java classes and interfaces. The data includes resources and other kinds of static information. We need to declare a module then add module-info.java at the root of the source code.

Below is the template of the "module-info.java" file.

<strong>module <module-name> {
   requires <module-name1> ;
   requires <module-name2>;

   exports <package-name1>;
   exports <package-name2>;

   exports <package-name> to <module-name>
}</strong>

We can use certain command-line options that help us to modify existing modules and add dependencies to them, export additional packages.

Below are the few command-line commands that can be used to modify an existing module.

<strong>1) --add-reads <module>=<target-module>(,<target-module>)*</strong>

The above command can update to read , regardless of the module declaration. can be ALL-UNNAMED to read all nameless modules.

<strong>2) --add-exports <module>/<package>=<target-module>(,<target-module>)*</strong>

The above command can update to export to , regardless of the module declaration. can be ALL-UNNAMED to export to all nameless modules.

<strong>3) --add-opens <module>/<package>=<target-module>(,<target-module>)*</strong>

The above command update  to open to , regardless of the module declaration.

<strong>4) --patch-module <module>=<file>(;<file>)*</strong>

The above command can replace or increase a module with classes and resources in jar files or directories.

Updated on: 2020-04-10T17:24:53+05:30

614 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements