Java 9 : How to create and run a custom Module

What is a Module in Java 9 ?

Java Modules are a part of Project Jigsaw.

A module is a named, self-describing collection of code and data. Its code is organized as a set of packages containing types, i.e., Java classes and interfaces; its data includes resources and other kinds of static information.

What are the Type of Modules in Java 9 ?

There are two types of Java Modules

  1. Java Standard Modules, they start with string java.
  2. Java Non-Standard Modules, they start with string jdk.

Creating a Custom Module.

We can make the following types of modules

  1. Module that is independent of all other modules.
  2. Module that requires other modules as dependencies.
  3. Module that exports itself for use by other packages.
  4. Module that imports some modules and exports some packages for other modules to use.

Note
When we are exporting the packages in modules, only the publically expoted modules are available to use by other modules.(We will get to this later in the post.)

A simple module that does not imports or exports any module may look as follows.

module com.ekiras { }

Creating an Independent Module

In this example we will be creating a simple module that will pring Hello World.

We will create two file for this example

src/com.ekiras/com/ekiras/Main.java
src/com.ekiras/module-info.java

Here, we will add the following code to the files.

module-info.java


module com.ekiras{

}

This module definition means that

  1. This module does not require any module to build itself.
  2. This module does not exports any thing for other modules to use.

Main.java


package com.ekiras;

class Main{
  
  public static void main(String args[]){
   System.out.println("Hello World by Ekiras");
  }

}

We will compile our code outside the src directory in mods folder (It can be any directory, if you specify any other directory just replace your directory name with mods for the rest of the post.)

Your directory structure should look as follows

|-path/
   |-mods/
     |-com.ekiras/
   |-src/
     |-com.ekiras/
       |-com/
         |-ekiras/
            |-Main.java
       |-module-info.java

Now, we will compile our module with the following command,

javac -d mods/com.ekiras/ src/com.ekiras/module-info.java src/com.ekiras/com/ekiras/Main.java 

Make sure you run this command from path/ and not anywhere inside this directory(or else you can change this command as required).

This will generate the following classes in mods/com.ekiras/ folder.

|-path
   |-com.ekiras/
     |-module-info.class
     |-com/
       |-ekiras/
         |-Main.class

Now, lets run this our first module with the following command

java --module-path mods -m com.ekiras/com.ekiras.Main
Hello World by Ekiras

Note

--modue-path : This refers to the path where module has been compiled. It is mandatory. Since we are running this from path "path/" we have given mods as the path. If we were running this from path "path/mods/", then our path would have been --module-path .

-m or --module : This refers to the module that we are going to run. Its param is module.name/package.MainClass.

How to Package a module

Now we will be packaging our module to a Jar file. To create a jar file we will use the jar tool of java. We can create a jar file with the following command.

jar --create --file libs/com.ekiras.jar --module-version=1.0 --main-class=com.ekiras.Main -C mods/com.ekiras .

Check the Jar Module Description

jar --print-module-descriptor --file=libs/com.ekiras.jar

This will result in the following output

com.ekiras@1.0
  requires mandated java.base
  contains com.ekiras
  main-class com.ekiras.Main

The above output states the following

  1. modulename is com.ekiras
  2. version is 1.0
  3. it requires the mandatory java.base module, since all modules are build on this module as a base.
  4. main class of the module is com.ekiras.Main

Run the jar file

Now, we can run the jar file with the following command

java -p libs -m com.ekiras

This will result in the following output, also in this case we do not have to specify the Main class.

Hello World by Ekiras

No comments:

Powered by Blogger.