How to build an executable file for a Java 11 GUI application using Launch4J
Before we look at the how perhaps we should ask the question of 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, what we are looking to deliver is our Java application along with the Java Runtime Environment and any external libraries required for it to run. To do this will need a few tools:
- 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 then you will have issues trying to run the Launch4J executable program that is installed. To run Launch4J open a cmd prompt and go to the directory where you have installed Launch4J and type “java -jar launch4j.jar” and press enter.
The Lanch4J application will open.
On the Basic tab
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”.
This 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 is located 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 good test. Ideally what you need is a machine that you know does not have Java 11 installed and test it on that machine.
To deploy to another machine, you will need to install
- Folder fxjre
The fxjre folder and its contents are the JRE your application requires to run; it is not wrapped into the executable file so you need to make sure it is added to the target machine along with the executable file. Do not change the folder name.