Java 9 introduced the module system to help solve issues with the classpath and JAR hell. In this article, we’ll take a closer look at the module system and how it works.
Modules are a way to organize Java code into groups of related classes and resources. A module is a self-contained unit of code that can be compiled, tested, and deployed independently. Modules can specify their dependencies on other modules, which makes it easier to manage the dependencies between different parts of an application.
To define a module, we create a module-info.java file in the root of our source code directory. This file contains information about the module, such as its name, version, and dependencies. Here’s an example module-info.java file:
module com.example.myapp {
requires java.base;
requires org.apache.commons.lang3;
exports com.example.myapp.service;
}
This module requires the java.base module and the org.apache.commons.lang3 module. It also exports the com.example.myapp.service package to other modules.
When we compile our module, we can use the javac command with the –module-source-path and –module options:
javac --module-source-path src -d out --module com.example.myapp
This command compiles our module, including its dependencies, and outputs the compiled code to the out directory.
Once we have our modules compiled, we can run them using the java command with the –module-path and –module options:
java --module-path out -m com.example.myapp/com.example.myapp.Main
This command runs our module, specifying the path to our compiled modules using the –module-path option, and the name of the module to run using the –module option.
One of the benefits of the module system is that it helps to avoid classpath conflicts. Previously, if two libraries depended on different versions of the same library, we could run into conflicts when trying to use both libraries together. With modules, each module specifies its dependencies explicitly, so we can avoid these conflicts.
Another benefit of the module system is improved security. Modules can specify which packages they export, which means that code outside of the module cannot access those packages. This helps to prevent unauthorized access to sensitive code.
In addition to the basic module system, Java 9 also introduced the concept of “jlink”, which allows us to create custom Java runtime images that include only the modules that we need for our application. This can help to reduce the size of our runtime and improve startup time.
Here’s an example of how we can create a custom runtime image using jlink:
jlink --module-path out --add-modules com.example.myapp --output myimage
This command creates a custom runtime image that includes our com.example.myapp module, along with all of its dependencies. The resulting image is stored in the myimage directory.
In summary, the module system introduced in Java 9 provides a new way to organize and manage Java code. Modules help to avoid classpath conflicts, improve security, and make it easier to manage dependencies between different parts of an application. While there is some additional overhead in defining and compiling modules, the benefits make it worthwhile for many projects.
It’s in fact very complicated in this busy life to listen news
on TV, therefore I simply use world wide web for that purpose, and take
the hottest information.