Search code examples
javajava-9java-platform-module-systemjlink

Optimize JVM modules using jlink for a fat Jar file


One for the coolest feature of JDK9 is jlink that makes a small optimized version of the JVM to run the application which is pretty useful especially to run the app in a container like a docker one. However, it's not very simple to just pass the JAR file you have and get an optimized runtime to run it.

Like I use Gradle and create a fat jar file (all the dependencies in a single jar file), and I just simply want to use this jar file to generate the JVM for it? Any solution?


Solution

  • Use JDeps to analyze your project's dependencies:

    jdeps --list-deps app.jar
    

    Then extract all platform modules (those starting with java. or jdk.) from that list and feed them into jlink:

    jlink --add-modules $REQUIRED-PLATFORM-MODULES --output jdk-for-app
    

    To verify:

    jdk-for-app/bin/java --list-modules
    

    This includes all platform modules you determined as well as their dependencies.