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
- Java Standard Modules, they start with string
java.
- Java Non-Standard Modules, they start with string
jdk.
Creating a Custom Module.
We can make the following types of modules
- Module that is independent of all other modules.
- Module that requires other modules as dependencies.
- Module that exports itself for use by other packages.
- 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
- This module does not require any module to build itself.
- 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
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
- modulename is com.ekiras
- version is 1.0
- it requires the mandatory
java.base module
, since all modules are build on this module as a base. - 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.
No comments: