1. Introduction
Project Jigsaw is an umbrella project with the new features aimed at two aspects:
- the introduction of module system in the Java language
- and its implementation in JDK source and Java runtime
In this article, we’ll introduce you to the Jigsaw project and its features and finally wrap it up with a simple modular application.
2. Modularity
Simply put, modularity is a design principle that helps us to achieve:
- loose coupling between components
- clear contracts and dependencies between components
- hidden implementation using strong encapsulation
2.1. Unit of Modularity
Now comes the question as to what is the unit of modularity? In the Java world, especially with OSGi, JARs were considered as the unit of modularity.
JARs did help in grouping the related components together, but they do have some limitations:
- explicit contracts and dependencies between JARs
- weak encapsulation of elements within the JARs
2.2. JAR Hell
There was another problem with JARs – the JAR hell. Multiple versions of the JARs lying on the classpath, resulted in the ClassLoader loading the first found class from the JAR, with very unexpected results.
The other issue with the JVM using classpath was that the compilation of the application would be successful, but the application will fail at runtime with the ClassNotFoundException, due to the missing JARs on the classpath at runtime.
2.3. New Unit of Modularity
With all these limitations, when using JAR as the unit of modularity, the Java language creators came up with a new construct in the language called modules. And with this, there is a whole new modular system planned for Java.
3. Project Jigsaw
The primary motivations for this project are:
- create a module system for the language – implemented under JEP 261
- apply it to the JDK source – implemented under JEP 201
- modularize the JDK libraries – implemented under JEP 200
- update the runtime to support modularity – implemented under JEP 220
- be able to create smaller runtime with a subset of modules from JDK – implemented under JEP 282
Another important initiative is to encapsulate the internal APIs in the JDK, those who are under the sun.* packages and other non-standard APIs. These APIs were never meant to be used by public and were never planned to be maintained. But the power of these APIs made the Java developers leverage them in the development of different libraries, frameworks, and tools. There have been replacements provided for few internal APIs and the others have been moved into internal modules.
4. New Tools for Modularity
- jdeps – helps in analyzing the code base to identify the dependencies on JDK APIs and the third party JARs. It also mentions the name of the module where the JDK API can be found. This makes it easier in modularizing the code base
- jdeprscan – helps in analyzing the code base for usage of any deprecated APIs
- jlink – helps in creating a smaller runtime by combining the application’s and the JDK’s modules
- jmod – helps in working with jmod files. jmod is a new format for packaging the modules. This format allows including native code, configuration files, and other data that do not fit into JAR files
5. Module System Architecture
The module system, implemented in the language, supports these as a top level construct, just like packages. Developers can organize their code into modules and declare dependencies between them in their respective module definition files.
A module definition file, named as module-info.java, contains:
- its name
- the packages it makes available publicly
- the modules it depends on
- any services it consumes
- any implementation for the service it provides
The last two items in the above list are not commonly used. They are used only when services are being provided and consumed via the java.util.ServiceLoader interface.
A general structure of the module looks like:
src |----com.baeldung.reader | |----module-info.java | |----com | |----baeldung | |----reader | |----Test.java |----com.baeldung.writer |----module-info.java |----com |----baeldung |----writer |----AnotherTest.java
The above illustration defines two modules: com.baeldung.reader and com.baeldung.writer. Each of them has its definition specified in module-info.java and the code files placed under com/baeldung/reader and com/baeldung/writer, respectively.
5.1. Module Definition Terminologies
Let us look at some of the terminologies; we will use while defining the module (i.e., within the module-info.java):
- module: the module definition file starts with this keyword followed by its name and definition
- requires: is used to indicate the modules it depends on; a module name has to be specified after this keyword
- transitive: is specified after the requires keyword; this means that any module that depends on the module defining requires transitive <modulename> gets an implicit dependence on the <modulename>
- exports: is used to indicate the packages within the module available publicly; a package name has to be specified after this keyword
- opens: is used to indicate the packages that are accessible only at runtime and also available for introspection via Reflection APIs; this is quite significant to libraries like Spring and Hibernate, highly rely on Reflection APIs; opens can also be used at the module level in which case the entire module is accessible at runtime
- uses: is used to indicate the service interface that this module is using; a type name, i.e., complete class/interface name, has to specified after this keyword
- provides … with ...: they are used to indicate that it provides implementations, identified after the with keyword, for the service interface identified after the provides keyword
6. Simple Modular Application
Let us create a simple modular application with modules and their dependencies as indicated in the diagram below:
The com.baeldung.student.model is the root module. It defines model class com.baeldung.student.model.Student, which contains the following properties:
public class Student { private String registrationId; //other relevant fields, getters and setters }
It provides other modules with types defined in the com.baeldung.student.model package. This is achieved by defining it in the file module-info.java:
module com.baeldung.student.model { exports com.baeldung.student.model; }
The com.baeldung.student.service module provides an interface com.baeldung.student.service.StudentService with abstract CRUD operations:
public interface StudentService { public String create(Student student); public Student read(String registrationId); public Student update(Student student); public String delete(String registrationId); }
It depends on the com.baeldung.student.model module and makes the types defined in the package com.baeldung.student.service available for other modules:
module com.baeldung.student.service { requires transitive com.baeldung.student.model; exports com.baeldung.student.service; }
We provide another module com.baeldung.student.service.dbimpl, which provides the implementation com.baeldung.student.service.dbimpl.StudentDbService for the above module:
public class StudentDbService implements StudentService { public String create(Student student) { // Creating student in DB return student.getRegistrationId(); } public Student read(String registrationId) { // Reading student from DB return new Student(); } public Student update(Student student) { // Updating sutdent in DB return student; } public String delete(String registrationId) { // Deleting sutdent in DB return registrationId; } }
It depends directly on com.baeldung.student.service and transitively on com.baeldung.student.model and its definition will be:
module com.baeldung.student.service.dbimpl { requires transitive com.baeldung.student.service; requires java.logging; exports com.baeldung.student.service.dbimpl; }
The final module is a client module – which leverages the service implementation module com.baeldung.student.service.dbimpl to perform its operations:
public class StudentClient { public static void main(String[] args) { StudentService service = new StudentDbService(); service.create(new Student()); service.read("17SS0001"); service.update(new Student()); service.delete("17SS0001"); } }
And its definition is:
module com.baeldung.student.client { requires com.baeldung.student.service.dbimpl; }
7. Compiling and Running the Sample
We have provided scripts for compiling and running the above modules for the Windows and the Unix platforms. These can be found under the core-java-9 project here. The order of execution for Windows platform is:
- compile-student-model
- compile-student-service
- compile-student-service-dbimpl
- compile-student-client
- run-student-client
The order of execution for Linux platform is quite simple:
- compile-modules
- run-student-client
In the scripts above, you will be introduced to the following two command line arguments:
- –module-source-path
- –module-path
Java 9 is doing away with the concept of classpath and instead introduces module path. This path is the location where the modules can be discovered.
We can set this by using the command line argument: –module-path.
To compile multiple modules at once, we make use of the –module-source-path. This argument is used to provide the location for the module source code.
8. Module System Applied to JDK Source
Every JDK installation is supplied with a src.zip. This archive contains the code base for the JDK Java APIs. If you extract the archive, you will find multiple folders, few starting with java, few with javafx and the rest with jdk. Each folder represents a module.
The modules starting with java are the JDK modules, those starting with javafx are the JavaFX modules and others starting with jdk are the JDK tools modules.
All JDK modules and all the user defined modules implicitly depend on the java.base module. The java.base module contains commonly used JDK APIs like Utils, Collections, IO, Concurrency among others. The dependency graph of the JDK modules is:
You can also look at the definitions of the JDK modules to get an idea of the syntax for defining them in the module-info.java.
9. Conclusion
In this article, we looked at creating, compiling and running a simple modular application. We also saw how the JDK source code had been modularized.
There are few more exciting features, like creating smaller runtime using the linker tool – jlink and creating modular jars among other features. We will introduce you to those features in details in future articles.
Project Jigsaw is a huge change, and we will have to wait and watch how it gets accepted by the developer ecosystem, in particular with the tools and library creators.
The code used in this article can be found over on GitHub.