Unveiling Java Jigsaw: Modular Programming in Java

Afsar Khan
3 min readMar 7, 2024

--

“Piece Together Your Java Projects: Mastering Modularity with Jigsaw”

Java Jigsaw

As we venture further into the modern era of Java development, a crucial piece remains at the forefront of our architectural designs and system modularity: Jigsaw. Introduced with Java 9 in 2017, Jigsaw aimed to make Java applications more scalable, maintainable, and secure. But years after its release, it’s worth revisiting the power and intricacies of Jigsaw. In this article, we’ll reacquaint ourselves with Jigsaw’s fundamental concepts and explore its impact on contemporary Java development through practical examples and code snippets.

What is Project Jigsaw?

Project Jigsaw’s goal was to make Java more scalable and flexible by breaking down the JDK into smaller, interdependent modules. This means instead of the monolithic rt.jar, Java now has a set of modules that can be independently compiled, packaged, and deployed.

These modules are named, self-describing collections of code and data and can export a certain API while keeping other parts of their code hidden and internal. Its goal is to let developers define modules that explicitly state their dependencies on other modules.

Let’s walk through how to create a simple modularized application with Java Jigsaw.

Creating a Modular Java Application

Step 1: Define Your Modules

Before we write any code, we need to define our modules. For this example, let’s create a simple application with two modules:

  • com.ak.greeter which provides a greeting
  • com.ak.main which contains our main method and uses the com.ak.greeter module

Each module will have its own directory reflecting its name.

Step 2: The module-info.java File

Each module requires a module-info.java file at the root of its directory. This file declares the module's name, its required modules, and which packages it exports.

For com.ak.greeter:

module com.ak.greeter {
exports com.ak.greeter;
}

And for com.ak.main:

module com.ak.main {
requires com.ak.greeter;
}

Step 3: Writing the Module Code

Now we’ll write a simple Greeter class in the com.ak.greeter module:

package com.ak.greeter;

public class Greeter {
public String greet() {
return "Hello, Modular World!";
}
}

In our com.ak.main module, we'll write the Main class:

package com.ak.main;

import com.ak.greeter.Greeter;

public class Main {
public static void main(String[] args) {
Greeter greeter = new Greeter();
System.out.println(greeter.greet());
}
}

Step 4: Compile and Run the Application

To compile the modules, navigate to the source directory containing both com.ak.greeter and com.ak.main directories and run:

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

This command compiles all .java files found in the source directory and places the resulting class files into the mods directory, preserving the module structure.

To run the application, use the following command:

java --module-path mods -m com.ak.main/com.ak.main.Main

You should see the output: “Hello, Modular World!”

Benefits of Java Modules

The benefits of using Java modules include:

  • Reliable Configuration: Dependencies are explicitly defined, making the system more reliable.
  • Strong Encapsulation: Internal APIs can be hidden from other modules, reducing the risk of accidental use.
  • Scalable Applications: Smaller runtime images can be created for applications, reducing their size and improving performance.

Challenges and Considerations

Adopting Java modules requires planning and consideration:

  • Existing applications need to be modularized, which can be a significant effort.
  • Libraries and frameworks need to provide module definitions to be fully utilized in a modular context.

Conclusion

Java Jigsaw is a powerful addition to the Java ecosystem, promoting better application structure, maintainability, and performance. As with any significant change, it presents challenges but also offers opportunities for developers to build better software.

Embrace the modularity, and may your code be clean and maintainable!

Keep up with my latest tech articles by following me on Medium.

--

--

Afsar Khan

"🚀 Distinguished Architect | AI Strategist | Tech Innovator & Leader | Sharing insights on generative AI, coding & tech trends. Let's explore the future!"