How to generate an executable of a modular Java app with javapackager?

tec picture tec · Jul 19, 2019 · Viewed 7k times · Source

Environment: Win10, Oracle JDK 9, Eclipse, jlink, javapackager

Context: I'm trying to generate an executable file with javapackager having a java custom runtime image made with jlink, reading the javapackager documentation. Not an installable.

Jlink created a zip file, containing all my java class files and resources encapsulated (it means I don't have access to them from System Explorer) + the custom jre. It comes with a .bat launcher to run the app. All works fine.

Walkthrow: 1st I found out that there is a jpackage utility of OpenJDK available from OpenJDK14 to make java modular app executable, which is stil under development. I didn't find any way to work with that. Later I investigated Inno Setup, but it doesn't fulfil my needs (because I didn't want an installer) Later I discovered that since Oracle JDK 8 there is javapackager tool to generate runnables for each plattform (max, linux, windows..).

I'm not able to make javapackager work. This is one of various attempts of the command I executed on Windows cmd.

javapackager 
-deploy 
    -native exe
    --add-modules myModule,javafx.graphics,javafx.controls 
    --module-path "C:\path\to\javafx-jmods;C:\path\to\target\classes;C:\path\to\more\jmods" 
    -outdir "C:\myApp" 
    -outfile MyApp 
    -appclass myPackage.MyApp 
     -name "MyApp"

With the previous command I get the error:

Error: No application jars foun

So, I realized I was mixing the -deploy command and its options with the -createjar command and its options.

Can someone explain how to generate a .exe with javapackager?

Edit 1

Tried jpackage (using jdk 14 with Wix installer needed as dependency):

jpackage --package-type exe -o outputdir --name myApp --add-modules myapp,javafx.graphics,javafx.controls 
--module-path "C:\path\to\some\jmods;C:\path\to\myTarget;C:\path\to\javafx-sdk-11.0.2plugin" -m myapp/App

Output: myApp.1.0.exe. It opens a cmd and a "installer", but does not executes myApp as there is not the custom java runtime environment included.

Answer

mipa picture mipa · Jul 19, 2019

Install the JDK provided with jpackage like any JDK and set your JAVA_HOME to it.

As a preparation for the packaging I have instructed Maven to copy all dependent jar files of the project into the installer/input folder.

Then go to the main folder of your project and call

$JAVA_HOME/bin/jpackage \
--name yourAppName \
--output installer/output \
--input installer/input \
--main-jar yourAppMain.jar \
--main-class xxx.yyy.yourAppMainClass

The result should now be in the installer/output folder.

For more detailed information about the options you can call

$JAVA_HOME/bin/jpackage --help

There is also an option to use a different JDK with the jpackager but this is more advanced.