Modularity in Java

Reading Time: 2 minutes

In this blog, I would be talking about Java Platform Module system aka Modules. Modules introduced as part of Java version-9, till Java version-8 we used jars files. As you guys know, Java-9 came out with lots of features and Java modules said to be one of the best feature of java -9. Module helps to minimise coupling, which also lead to easier to maintain code.

JAR is a file format that enables you to bundle multiple files into a single archive file.” whereas “module is a named, self-describing collection of code and data. Its code is organised as a set of packages containing types, i.e., Java classes and interfaces; its data includes resources and other kinds of static information.

What is Java Module ?

It is just a collection of related packages and resources with another file “module descriptor” which contains information related to module. In Java module, we can also define access modifiers for a particular class or package means which piece of code is publicly accessible or outside the module. We define this information in module descriptor file. You will find module descriptor file in every java module. We can also create out custom Java module for any use case and for that first we need to understand what information kept in the module descriptor file.

The naming convention of modules is same as package naming conventions. In modules, all the packages, by default is private, by using module descriptor file we can manage the accessibility.

How to create a Java Module?

Step-1. Create script module and define a directory structure in module folder , i.e . script/com/knoldus

Step-2. Create the module descriptor file for the script module.

module script {


This is minimum information required to create a new custom module in java.

Step-3. Create a new Java class ModuleDemo inside script/com/knoldus directory structure. Don’t forgot to add the package name in the 1st line of the Java class.

package com.knoldus

class ModuleDemo{

public static void main(String[] vars) {

System.out.println(“Hello World”);



Step-4. Now we are done with the code and structure of the module, we just need to compile the module code, for that we would be making us of the below command.

javac --module-source-path ~/Desktop/src/ -d ~/Desktop/moduleOutput -m script

Here we are storing the compiled module component in moduleOutput folder. If you open the moduleOutput folder there you would observe the classes in .class format and file structure would be same as the original source code of the module.

Step-5. Now let’s run the compiled module by the help of below mentioned command where we have give full directory path where java file is residing.

java –moule-path ~/Desktop/moduleOutput/ -m script/com.knoldus.ModuleDemo

As output “Hello world” would print on the terminal.

That’s pretty much it from the article. If you have any feedback or queries, please do let me know in the comments. Also, if you liked the article, please give me a thumbs up and I will keep writing blogs like this for you in the future as well. Keep reading and Keep coding.


Java 9 documentation
Oracle reference

Written by 

Lokesh Aggarwal is a software Consultant trainee with 6 months of experience at Knoldus Inc.