Skip to content

Bundle JAR into universal executables

Alexandre edited this page May 3, 2021 · 3 revisions

What about the project ?

The project is built using JDK 11 thus, launching it using previous Java version is impossible. We recommend using JDK 11 for the following steps.

/!\ For this project only !

Creating our custom JRE

Tools used :

  • jdeps*

  • jlink*

    * installed with JDK 11 for example

Java is now a modular platform, where you can create your own "JRE" distribution with specifically the modules that you need to run your application.

So instead of packing a full JDK folder beside the application, we'll create our own JRE folder containing all the modules we need.

We assume you have built or retreive the project's artifact .JAR Application.

Next, go into the folder containing the artifact using your terminal.

$ jdeps --module-path lib/ --add-modules=ALL-MODULE-PATH --list-deps java-rdp.jar

It will return the list of modules which are used in our application.

Example:

  java.base
  java.desktop

We can now use the tool jlink to create our JRE.

jlink --no-header-files --no-man-pages --add-modules <MODULE1>,<MODULE2>,... --output <CUSTOM JRE DIR>

Now, a <CUSTOM JRE DIR> directory will be created! That's it !

Size of :

  • full JDK 11 : 285 MB (👎)
  • custom JRE : 72 MB (🥇)

Wrapping it for Windows

Tools used :

Unfortunately, Launch4j doesn't wrap the JRE in the .exe application, you have to put it next to it. I will explain :

  1. Create a folder where you want, that's where we will be putting our application, we'll call it <APP FOLDER>.
  2. Open Launch4j (Java 1.8.0 required i think)
  3. Choose your output file (in <APP FOLDER>) (don't forget to put .exe at the end...)
  4. Choose your JAR file
  5. Next, in the JRE tab, just type <CUSTOM JRE DIR> (for example, jre_custom if we named our custom JRE folder that way)
  6. In the 'Classpath' tab, click on Custom classpath and choose the main class.
  7. You can then put an icon, or version information but it's not necessary !
  8. Then, press the "settings" icon beside the run button. It will ask you to save a .CFG file, put it where you want and it'll generate the .EXE app in <APP FOLDER>
  9. Next, copy the <CUSTOM JRE DIR> into the <APP FOLDER> folder

Your <APP FOLDER> should look like :

<APP FOLDER>:
   |-- <CUSTOM JRE DIR>
      |--bin/ (in bin there is java.exe)
   |-- lib/
   |-- <APP>.exe

Thus, you should be able to distribute the application and the client doesn't even need to have JDK installed.

Wrapping for Linux and MacOS

You can launch the .JAR application using command line using the custom JRE folder and binary.

Inside the application folder :

<CUSTOM JRE DIR>/bin/java --module-path lib/ --add-modules=ALL-MODULE-PATH -jar java-rdp.jar

Problems ?

Simply create a New Issue and I will try to help you.