Java Module System – How to create a module, compile it and run the modular java application from a command line.

In this post, I am going to tell you how to create a module. How you can compile that module and use modular architecture to run java application.

you can watch me code the module-system mentioned in this post on youtube.

I am using java 16 and ubuntu 18.04. You can run commands which are suitable for your operating system.

First create a folder where source code of your module is going to be put.

mkdir module-source

then move in that folder

cd module-source

then create a folder, which will have the same name as your module. A module should be in a folder, which has the same name.

mkdir test.module

then go inside the test.module folder.

cd test.module

And create a file in it.

module test.module{}

Create one folder test for having one java package.

mkdir test

Move in the test package folder, and create a file in it.

package test;

public class Test{
    public static void main(final String ...args){
        System.out.println("***Hello from test.Test class inside the test.module***");

Now, move outside of module-source folder and create a hierarchy of folder to store compiled module classes.

The parent folder will be module-classes  and it is going to have a child test.module where java compiler will put compiled classes of module test.module.

mkdir -p module-classes/test.module

Then run below command to compile the module.

javac --module-source-path module-source -d module-classes $(find module-source-path -name "*.java")

Make sure you are running the compile command from the parent of module-source .

Then run below command to run the modular java application.

java -p module-classes -m test.module/test.Test

-p – needs location of folder where the modules are kept

-m – needs name of the module and the class name inside that module which has the main function in it.

You can browse the code on github.