Issue
For some of my projects I'm using JavaFX for the GUI, and prior to JavaFX being split as OpenJFX I didn't had any problems with it. Now I want to use the latest versions of both JDK and JavaFX, so because of this some changes had to be made.
While working in Eclipse, everything is running as it should, I added JavaFX as a library, but I have some problems when I try to create an exe file from the runnable jar using Launch4j. In Eclipse I'm using 2 VM arguments:
- --module-path "Path to the lib folder\" --add-modules javafx.controls,javafx.graphics,javafx.fxml
- this is for Caused by: java.lang.NoClassDefFoundError: javafx/application/Application
- --add-opens javafx.graphics/com.sun.glass.ui=ALL-UNNAMED
- this is for cannot access class com.sun.glass.ui.Window (in module javafx.graphics) because module javafx.graphics does not export com.sun.glass.ui to unnamed module
With the second argument I don't have any problems, as it doesn't require a path to a lib, but the first one does, and I can't simply use a static path (like: C:\MyAppName\lib) because the user may install the app where he wants to, and there will be the lib also. That being said, what can I use as a relative path, so Launch4j knows what to do? The lib folder is in the same location as the exe.
I tried the following but with no luck (I've added them in JVM options box):
- --module-path "%EXEDIR%\lib" --add-modules javafx.controls,javafx.graphics,javafx.fxml
- --module-path "./lib" --add-modules javafx.controls,javafx.graphics,javafx.fxml
- --module-path "\lib" --add-modules javafx.controls,javafx.graphics,javafx.fxml
The question maybe has some details that may not be related to what I need, but maybe someone else has the same problem, and I wanted to add them to maybe help them too when I get an answer.
Solution
Following @James_D advice, I actually used jpackage
instead of Launch4j
and it is better, not only that it creates your exe file, but it also removes the need from the user to install Java by creating a native package in a platform-specific format.
That being said, this is a small guide on Windows 10 OS on how to use jpackage
with a JavaFX project. For more information read the documentation. jpackage
is available for macOS and Linux, but I'm using Windows 10, so the guide is specific to it.
- Go to the following link (official) and download JavaFX Windows jmods;
- For
jpackage
you also need to download WiX Toolset, and install it; - Create a runnable Jar for your project. I found that using runnable Jar instead of normal jar is easier because you don't have to specify later the main class. This is a personal preference and not what you have to do;
- Try running
jpackage -h
in command line/Windows PowerShell to see ifjpackage
is working. If you get The term 'jpackage' is not recognized as the name of a cmdlet, then open Environment variables and in System variables/Path add the location of bin folder from your Java JDK. An easy way to do it, is by searching for environment in Windows search bar, and selecting Edit the system environment variables, then Environment variables... - After this you can run:
jpackage -t exe --name "Name of app" --app-version 1.0 --input "Location of runnable Jar" --dest "Location where the exe will be created" --main-jar "Name of the runnable Jar, with extention .jar at the end" --icon "Complete location where the icon of the app is .ico" --module-path "Location of jmods folder" --add-modules javafx.controls,javafx.graphics,javafx.fxml,javafx.base --win-shortcut --win-menu
Example:
jpackage -t exe --name "MyApp" --app-version 1.0 --input "D:\Projects\Jars\" --dest "D:\Projects\Executable" --main-jar "Guide.jar" --icon "D:\Projects\Icons\guide.ico" --module-path "D:\Projects\JavaFX\javafx-jmods-16" --add-modules javafx.controls,javafx.graphics,javafx.fxml,javafx.base --win-shortcut --win-menu
- In my case I am using my personal installer app, so what I'm doing after running
jpackage
, is that I'm running the exe file it is created, and then from the folder it creates, I make a copy of the files from that folder (the exe file, app and runtime folder) and paste them in the location with all other necessary files for my project. This way I can test it, and if it works I'm moving forward to create the installer for it, like I said, using my own installer app.
This command has a lot more options you can add to it, so read the documentation, and also the complete list of options by running jpackage -h
.
Answered By - sValentin