Modules

Java Module System is a major change in Java 9 version. Java added this feature to collect Java packages and code into a single unit called module. In earlier versions of Java, there was no concept of module to create modular Java applications, that why size of application increased and difficult to move around. Even JDK itself was too heavy in size, in Java 8, rt.jar file size is around 64MB. To deal with situation, Java 9 restructured JDK into set of modules so that we can use only required module for our project. Apart from JDK, Java also allows us to create our own modules so that we can develop module based application. The module system includes various tools and options that are given below.

  1. Includes various options to the Java tools javac, jlink and java where we can specify module paths that locates to the location of module.
  2. Modular JAR file is introduced. This JAR contains module-info.class file in its root folder.
  3. JMOD format is introduced, which is a packaging format similar to JAR except it can include native code and configuration files.
  4. The JDK and JRE both are reconstructed to accommodate modules. It improves performance, security and maintainability. Java defines a new URI scheme for naming modules, classes and resources.

Module is a collection of Java programs or softwares. To describe a module, a Java file module-info.java is required. This file also known as module descriptor and defines the following

  1. Module name
  2. What does it export
  3. What does it require

It is a name of module and should follow the reverse-domain-pattern. Like we name packages, e.g. com.javatpoint. Creating Java module required the following steps.

  1. Create a directory structure
  2. Create a module declarator
  3. Java source code

To create module, it is recommended to follow given directory structure, it is same as reverse-domain-pattern, we do to create packages/project-structure in Java.

Figure 4.7: Directory for Java module file.
Image java-module

Create a file module-info.java, inside this file, declare a module by using module identifier and provide module name same as the directory name that contains it. In our case, our directory name is com.javatpoint.


\begin{lstlisting}
module com.javatpoint{
\par
}
\end{lstlisting}

Leave module body empty, if it does not has any module dependency. Save this file inside src/com.javatpoint with module-info.java name. Now, create a Java file to compile and execute module. In our example, we have a Hello.java file that contains the following code.


\begin{lstlisting}
class Hello{
public static void main(String[] args){
System.out.println(''Hello from the Java module'');
}
}
\end{lstlisting}

Save this file inside src/com.javatpoint/com/javatpoint/ with Hello.java name. To compile the module use the following command.


\begin{lstlisting}
javac -d mods --module-source-path src/ --module com.javatpoint
\end{lstlisting}

To run the compiled module, use the following command.


\begin{lstlisting}
java --module-path mods/ --module com.javatpoint/com.javatpoint.Hello
\end{lstlisting}

Output:


\begin{lstlisting}
Hello from the Java module
\end{lstlisting}

To see the compiled module descriptor use the following command.


\begin{lstlisting}
javap mods/com.javatpoint/module-info.class
\end{lstlisting}

This command will show the following code to the console.


\begin{lstlisting}
Compiled from ''module-info.java''
module com.javatpoint {
requires java.base;
}
\end{lstlisting}

See, we created an empty module but it contains a java.base module. Why? Because all Java modules are linked to java.base module and it is default module.