JPMS in Java9


Java Platform Modular System is commonly known as JPMS.

The concept of JPMS i.e. Java Platform Module System came in Java 9. Its development was first started in 2005 and finally in 2017, this concept came under the project named Jigsaw.

Until Java 8, we used jar files, but from Java 9 onwards, we will be using modules.

Modularity is the basic rule of good software engineering practices. This is a technique to tackle complexity of both design and maintenance of a software product. Java9 developers, took this recipe which finally changes the paradigm of programming.

Let’s first understand the difference between a jar file and a module. As the official statement says “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 organized as a set of packages containing types, i.e., Java classes and interfaces; its data includes resources and other kinds of static information.

A package is a collection of classes, enums, interfaces. A jar is a group of these packages. A module also contains these packages but apart from that it contains a module descriptor, a file module-info.class that was compiled from a

What does contains and how to create it ? is a file which contains information such as :

  • What are the modules required by this module using the statement :
require moduleName;
  • What packages are exported by this module using the statement :
export packageName;

For example, you have two modules named as “moduleA” and “moduleB“. “moduleA” has three packages say “packageA“, “packageB“, “packageC“. Now, if “moduleA” requires “moduleB” and you want to export only “packageC” to the outside world, this is how your will look like :

module moduleA {
	requires moduleB;
	export packageC;

How to create module ?

Since now we are aware about how to create a file, let’s start creating our own module. We will be creating a very basic module. As we know, a module contains packages and file. And packages contain java files.

Step 1 : Create a folder by the name of your module, say moduleA inside source folder say src using command:

> mkdir moduleA

Step 2 : In order to create a package named as com.knoldus, inside that folder moduleA create a folder inside folder with names com (outer folder) and knoldus (inner folder) :

> cd moduleA/
> mkdir com
> cd com/
> mkdir knoldus
> cd knoldus/

Step 3: Now create a Java file inside this package and write your required functionality, say, to write the statement :

package packageName;

For now, let us have a simple class printing statement “Hello World” as shown below :

package com.knoldus;

class Demo {
 	public static void main(String[] vars) {
		System.out.println("Hello World");	

Step 5 : This is the most important step. We need to create inside the folder moduleA. For now, we have just one package in our module and let’s assume that we don’t want to expose this to the outside world. Also, we don’t require any other module for now. So our will look like :

module moduleA {

This is the minimum information required in a file to create a module.

Now we are done creating a module. Let’s see the tree structure of the src folder created using the command :

> tree

This should look like :

Now we are ready to compile our module using command mentioned below:

javac --module-source-path ~/Desktop/src/ -d ~/Desktop/out -m moduleA

According to the above command, my output folder is named as out. Let’s verify the tree structure of our output folder.

The structure seems similar to the previous except a few changes i.e. in the source folder, we had .java files and now after compilation, we got .java files corresponding to each .class file as expected.

Now, we are done with compilation, so let’s run our first module using the command mentioned below. Now our module path should have the name of the output folder i.e. out. The output is Hello World, as expected.

java --module-path ~/Desktop/out/ -m moduleA/com.knoldus.Demo

Now, you are ready to play with modules!

Written by 

Jasmine is a Software Consultant, with an experience of more than 11 months. She is familiar with Object Oriented Programming Paradigms and has a good command over C/C++. Apart from being a programmer, She is also fond of relational database technologies such as PostGreSQL & MySQL. Apart from that, work life balance is one of the most important thing for her. Her hobbies include watching netflix,chatting ,surfing net and writing.

Knoldus Pune Careers - Hiring Freshers

Get a head start on your career at Knoldus. Join us!