Java

Slim modular Java 9 runtime Docker image with Alpine Linux

Posted on by  
Riccardo Lippolis

With the release of Java 9, and the introduction of Project Jigsaw (the Java Platform Module System), we no longer have the need for a full-blown JRE to run our Java applications. It is now possible to construct a stripped-down Java Runtime, containing the minimum set of required modules. This allows us to create slim Docker containers without excess baggage. The source code belonging to this blog post can be found at: https://github.com/rlippolis/java9-runtime-image

Our example application consists of two Java 9 modules (basically two JARs, with a module-info.java). We assume the concept of modules is familiar to the reader. If not, you can learn more about it e.g. here: http://openjdk.java.net/projects/jigsaw/ Firstly, we have a backend module consisting of a class which provides us with a String (to keep it simple). The backend module has no explicit dependencies on other modules (only an implicit dependency on java.base). Secondly, we have a frontend module consisting of an executable main class. This class gets the String from the backend and prints it to System.out (again, very straightforward). This module has an explicit dependency on backend, and an implicit dependency on java.base. To see the application in action, build it with Maven (mvn clean package), and run it from the command line:

(or use the provided run-app.sh) The -p option sets the module path (similar to the 'old' classpath). The -m option specifies the module and class to run.

Continue reading →

shadow-left