javamavenexecutable-jarjlink

How to create JRE to run jar programs?


I have an executable-jar that I can run with java -jar app.jar but the SDK is 326MB. This is a lot.

jlink can create a JRE, but I can't use jlink as I have a non-modular application.

Can you please tell me how to create a JRE?


Solution

  • jlink can be used to create a 'JRE'/runtime image for non-modular applications just fine. It just can't automatically derive the modules that should go in the runtime image in that case. They have to be specified manually instead.

    For instance, if I have a simple app.jar:

    $ java -jar app.jar
    Hello World!
    

    Then create a runtime image with jlink, with only the java.base module:

    jlink --output runtime --add-modules java.base --strip-debug --no-header-files --no-man-pages
    

    Then I can run the jar with the java executable in the runtime image:

    $ ./runtime/bin/java -jar app.jar
    Hello World!
    

    And the runtime image is just ~35 MB (though this can vary per platform).


    jdeps can be used to get an idea of which modules should be used to create the runtime image:

    $ jdeps --print-module-deps add.jar
    java.base
    

    This will print a comma-separated list of modules that can be passed directly as an argument to the --add-modules option of jlink. (though, service interface implementations have to be handled separately. See e.g.: jdeps does not add jdk.random when using RandomGenerator.getDefault())