Technical Article => Programming => Java

Java 9 is going to introduce a disruptive change to the Java platform -- Module System. The module system will change how Java applications work in the future. It's like changing the foundation of a house without impacting the house functionality and its top level structure. This obviously is a big challenge for the whole Java community.

To bring as little pain as possible to migrate existing applications to Java 9 without refactoring the whole application, Java 9 will introduce a few different module types. In following sections, these different module types will be covered.

But before covering these module types, you have to understand what the new module system can achieve.

Reliable configuration . This ensures that a specific class can be found and loaded easily in its execution path since a module explicitly declares the modules it requires/reads and it only needs to find the module to find the class. It eases the pain of searching through the classpath to find and load a class.

. This ensures that a specific class can be found and loaded easily in its execution path since a module explicitly declares the modules it requires/reads and it only needs to find the module to find the class. It eases the pain of searching through the classpath to find and load a class. Strong encapsulation. This is ensured because the module declared will have to explicitly export its packages so that other modules can access. The packages which are not exported explicitly cannot be accessed outside of the module.

Now let's get to know the three types of module in Java 9.

Named module

A named module is a module created with a module declaration file module-info.java in its root folder. The module-info.java is just like a normal Java class file and it usually has below structure.

module module_name { requires other_module_1; requires other_module_2; exports package_name_1; exports package_name_2; }

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 happen. It also indicates it will access packages in other_module_1 and other_module_2. The requires ensures reliable configuration.

Also a module needs to export some packages which other modules can access. This ensures strong encapsulation. You can also export a package to a specified module by declaring something like

exports package_name to other_module;

This is called qualified exports. It means only other_module can access the package_name, no other modules can access it.

Unnamed module

An unnamed module is a jar built without module-info.java declaration. This means all current jars built in Java 8 and earlier releases are unnamed modules.

An unnamed module will require all other modules and will export all its packages as well. This provides great convenience to current applications to use Java 9. Since an unnamed module can read all other modules, this doesn't require current application to be modularized before using Java 9.

One thing to note is an unnamed module exports all its packages doesn't mean a named module can access an unnamed module packages. This is because there is no way in module-info.java to declare requires unnamed_module. Java 9 doesn't allow named modules to read unnamed modules. This only ensure other unnamed modules can access its packages.

Automatic module

What if a named module wants to read the existing jars built without module-info.java created? Automatic module can be a great help in this case. The only thing you need to do is to put the jar file in the module path instead of the class path. Once a jar without module-info is put in the module path, it becomes an automatic module automatically.

An automatic module can read all other modules and all other modules can read it as well. Even the named modules can read an automatic module. An automatic module will have a module name derived from its jar name.

Conclusion

Module system is a big change to Java platform. It requires lots of effort to make it work properly for as many as applications. It requires lots of changes to existing tools, IDEs, frameworks to adapt to this new design. But it will definitely bring you much good in the future. And to help users migrate to Java 9 without causing much trouble, Java 9 introduces these different module types for free.

For more information about Java 8 and module system. Below are some useful links :