How to build an executable file for a Java 11 GUI application using Launch4J
Before we look at the how we should ask why. The aim is to package up our Java application so that we can distribute it to others. We want them to be able to launch the application without having to install anything else.
Therefore, we are looking to deliver our Java application along with the Java Runtime Environment and any external libraries required for it to run. To do this needs a few tools:
Prerequisites:
- JRE 11 (see Using JLink to build a Java 11 JRE)
- Launch4J (I’m using version 3.14), an executable JAR file
- An executable JAR file
Assuming you have Java 11 installed on your machine and it is the default Java installation, you will have issues trying to run the Launch4J executable program as installed. To run “Launch4J”, open a cmd prompt, go to the directory where you have installed Launch4J, type “java -jar launch4j.jar”, and press enter.
The Lanch4J application will open.
On the Basic tab
To specify an output file, I used “C:\Temp\sun2\sun.exe”.
Specify the location of the JAR file. I used “C:\Temp\sun2\app2\sunlounger.jar” I chose to use an icon for my application, but this is optional. I used “C:\Temp\sun2\sunbed.ico”.
On the Header tab
Make sure the Header type radio button for GUI is selected
On the JRE tab
Specify the Bundled JRE paths. I used “fxjre”.
Specifying the JRE path does not package the JRE into the executable file; the executable file will look for a folder called “fxjre” relative to where the executable file resides, and this should contain the JRE.
Specify the Min JRE version. I used 11.0.2.
That’s all you need to do. Press the gear icon to build the executable file.
To check your application runs, double-click the sun.exe file, and the application should launch.
However, you have JDK 11 installed on your machine, so this is not a valid test. Ideally, you need a machine that does not have Java 11 installed and test it on that machine.
To deploy to another machine, you will need to install
- Sun.exe
- Folder fxjre
The fxjre folder contains the JRE your application requires to run; add this to the target machine with the executable file. Do not change the folder name.