build.gradle
apply plugin: "java" sourceCompatibility = 1.9
Now let's add module-info.java and put it in the root project.
module jigsaw { }
Your project structure should look like this.
|-- build.gradle `-- src `-- main `-- java |-- jigsaw | `-- Main.java `-- module-info.javaLet's now build the JAR using Gradle.
gradle
Type the following command to run the application.
java --module-path build/libs -m jigsaw/jigsaw.Main
We can still run the JAR with a module system using a standard classpath.
java -cp build/libs/jigsaw.jar jigsaw.Main
In order to make it easy to distribute the application and to reduce the Java runtime size, we can create a custom Java runtime image using jlink.
jlink --module-path "build/libs;$JAVA_HOME/jmods" --add-modules jigsaw --output jigsaw
The command above creates a Java runtime image in the directory called jigsaw.
jigsaw |-- bin |-- conf |-- legal |-- lib `-- release
By using jlink, the jigsaw module that we just created will be part of the Java runtime image. You will not see the jigsaw JAR gets in the Java runtime image. If you run this command below, you will see the jigsaw module is now part of the image.
$ cd jigsaw $ bin/java --list-modules java.base@9.0.1 jigsaw
We can now run the jigsaw module by running the Java executable inside the Java runtime image without having to specify -m option.
$ cd jigsaw $ bin/java -m jigsaw/jigsaw.Main