What are the different compilation modes of a module in Java 9?

A module is a container of packages and each module contains a module descriptor that includes module name, module dependencies, it means that the name of other modules depends on and name of the packages it exports that can be used only by modules that depend on it.

module com.tutorialspoint.app {
   /** Modules upon which the module com.tutorialspoint.app depends on */
   requires com.tutorialspoint.services;
   /** Packages exposed by this module which can be used by other modules */
   exports com.tutorialspoint.app.util;

There are three different compilation modes provided by Java 9 Module: Legacy mode, single module mode, and multi-module mode.

Compilation modes of a Module:

  • Legacy Mode: It can be enabled when the compilation environment defined by the --source, --target, and --release options is less than or equal to 8. The compiler behaves the same way as it does in Java 8 (or before) where we can use traditional options (classpath, etc.) rather than any of the modules related options (--module-path). In this mode, our code runs as the unnamed module during runtime.
  • Single Module Mode: It can be enabled when the compilation environment is 9 or later and the --module-source-path option is not used. In this mode, the code has structured in a traditional package hierarchical directory tree. The code has a module-info.java file and runs on modulepath rather than on classpath. In this structure, we can place our module-info.java file directly under the src directory. We can't have multiple module-info.java files in the same directory tree, so it's called a single module mode.
  • Multi-Module Mode: It can be enabled when the compilation environment is 9 or later and the --module-source-path option is used. In this mode, we place multiple modules under the same source directory. During the compile-time, the main source directory can be specified with the --module-source-path option. The source tree for each individual module can be placed in its own subdirectory under the main source directory.

Updated on: 03-Mar-2020


Kickstart Your Career

Get certified by completing the course

Get Started