Over a million developers have joined DZone.

Java 9 Kickoff: Project Jigsaw (Part I)

Today’s monolithic applications come in micro batches. Every micro batch is independent from one another. Now, Java will come in this flavor using project Jigsaw

· Java Zone

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

Java 9 comes with a great feature called "Jigsaw," which modularizes monolithic Java code into modules, allowing us to design independently standard modules within different scopes. The primary goal of “jigsaw” is to enhance scalability, maintainability, performance, security, etc. For more information please click on this link.

Today, we're creating a greeting, using project “Jigsaw” to modularize our code independently.

Prerequisites

Step 1

Create a source directory for our source code:

$ mkdir -p jigsaw-sample1/src

Step 2

To categorize our system into modules like in this example, I have one utility module and another main module. The utility module contains utility classes, and the main module accesses that utility class and their methods to performing operations.

jigsaw-sample1/src $ mkdir com.knoldus.util com.knoldus.main

Before Jigsaw, we modularized our project using packages, but still we had a monolithic structure. Our package conventions reverse the domain of the company. Jigsaw still recommends that convention.

As per the above command, we are creating two directories, which is basically our project modularization. These are two independent modules and for packaging — two independent JARs, which we will see later.

Step 3

Create a package structure within our module like com.knoldus.util directory contains directories com/knoldus/util.

jigsaw-sample1/src/com.knoldus.util $ mkdir -p com/knoldus/util
jigsaw-sample1/src/com.knoldus.util $ cat > module-info.java

The module-info.java contains the information of the module and defines the scope of module as below:

module com.knoldus.util {
    exports com.knoldus.util;
}

In the above code, we are using exports so that other modules can use this module. Even if the scope of the class is public and we are not defining the scope of the utility module for exports, other modules will not able to access utility classes. If this module requires some other modules, then we need to define them here, which we will see later.

Step 4

Like the above code, we are creating the package structure of our main module and module-info.java for module information.

jigsaw-sample1/src/com.knoldus.main$ mkdir -p com/knoldus/main
jigsaw-sample1/src/com.knoldus.main$ cat > module-info.java

The module-info.java contains the following code:

module com.knoldus.main {
    requires com.knoldus.util;
}

In the above module code, we are using requires instead of exports because we are using our utility module in the main module. For accessing classes, we need to define our required module here.

Step 5

Create a utility class: jigsaw-sample1/src/com.knoldus.util/com/knoldus/util$ cat > Greeting.java 

with the following code:

package com.knoldus.util;

public class Greeting {
    private Greeting() {}

    public static String greetings(String user) {
        return user + " greetings !!! ";
    }
}


Create a Main class:

jigsaw-sample1/src/com.knoldus.main/com/knoldus/main$ cat > Main.java


With the following code:

package com.knoldus.main;

import static com.knoldus.util.Greetings.greetings;

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

Step 6

The compilation using “Jigsaw” is very different. The byte-code is divided into modules. If you already have previous versions of Java in the system, export JDK-9 temporarily into the terminal as below:

export JIGSAW_HOME=/opt/jdk-9/
export JIGSAW_BIN=/opt/jdk-9/bin/

Run the following command to compile the Java code:

jigsaw-sample1$ mkdir mods
jigsaw-sample1$ $JIGSAW_BIN/javac -d mods --module-source-path src $(find src -name "*.java")
jigsaw-sample1$ $JIGSAW_BIN/java -p mods -m com.knoldus.main/com.knoldus.main.Main
Hello Knoldus greetings !!!

Step 7

When we deploy our Java applications, we are using JAR packaging for easy deployment and distribution. With Jigsaw, the following is the way to package our modules and execute the JAR:

jigsaw-sample1$ mkdir mlib
jigsaw-sample1$ $JIGSAW_BIN/jar --create --file=mlib/com.knoldus.util@1.0.jar --module-version=1.0 -C mods/com.knoldus.util .
jigsaw-sample1$ $JIGSAW_BIN/jar --create --file=mlib/com.main.jar --main-class=com.knoldus.main.Main -C mods/com.knoldus.main .
jigsaw-sample1$ $JIGSAW_BIN/java -p mlib -m com.knoldus.main

Note: To download the source code of the above example, please click here. 

For more about Java 9 features, stay tuned, we'll be coming out with more blogs soon.

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:
monolithic ,java ,project jigsaw ,modular

Published at DZone with permission of Harmeet Singh(Taara). See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}