Since Java 9, there is no longer a Java Runtime Environment (JRE) shipped as part of the Java Development Kit. The JDK provides a tool to allow you to create your own JRE. In this article, we will look at using this tool to create our own JRE.
- OpenJDK 11
- JavaFX 11 jmods
- Your runnable JAR file
- Any external libraries you want to be included in the JRE
We will use JLink which comes with JDK11. In this build, we will create a JRE that includes JavaFX so if you want to run a basic JavaFX application you can.
To do this we need to set some variables to the key files we will need. We will need: –
- Location of Java JDK
set PATH_TO_JAVA=”C:\Program Files\java\jdk-11.0.2″
- Location of JavaFX modules
set PATH_TO_FX=”C:\Program Files\java\javafx-jmods-11.0.2″
- Location of any external JARs your applications use by default such as logging
Above, I have set the path to the various installed products. When calling JLink we will need to access the JDK modules folder. Therefore we point at the jmods folder provided as part of the JDK.
In my example, I am running a JavaFX application so I will need to include JavaFX modules as well. The JavaFX modules can be downloaded separately from the Gluon HQ
The last part of the JLink command is to add all the modules used in the modules.info file of your application. In this example, I have only used those that I know I need but there is nothing stopping you from adding more so you can use the JRE for a multitude of applications.
jlink --module-path %PATH_TO_JAVA%\jmods;%PATH_TO_FX% --add-modules=javafx.controls,javafx.fxml,javafx.graphics,java.desktop,javafx.base --output stdjre
Once the command completes you should find you have a new folder called stdjre which contains the JRE. To prove that your JRE is built go into the stdjre/bin folder and type the following command
The response you get will be something similar to this
openjdk version "11.0.2" 2019-01-15 OpenJDK Runtime Environment 18.9 (build 11.0.2+9) OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)
That’s it you now have a JRE you can use with your applications!