2

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:

  1. --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
  1. --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.

double-beep
  • 5,031
  • 17
  • 33
  • 41
Devorious
  • 107
  • 1
  • 8
  • You could just use IntelliJ, it's so much better than Eclipse, and it comes with an integrated option for JavaFX projects. Which workes almost (you still have to import the library) right out of the box even for JDK 15 (https://www.jetbrains.com/idea/) It also features a great debugger, that'll help you find problems in your code way faster, so why are you making your life harder using Eclipse? – IntoVoid Mar 30 '21 at 15:03
  • The best approach is to create a native bundle, which includes a JVM. Using `jlink` you can create a JVM which includes the JavaFX modules. The `jpackage` tool, included with JDK 14 and later, goes one step further and creates the native bundle with the "customized" JVM (so it essentially combines the functionality of `jlink` and Launch4j. See https://docs.oracle.com/en/java/javase/14/jpackage/packaging-overview.html – James_D Mar 30 '21 at 15:28
  • @James_D `jpackage` does look really great, and it does what I need, the only problem that I have is that when I try to use it I get **The term 'jpackage' is not recognized as the name of a cmdlet**. I did found examples of what to use for the command, but not how to actually be able to use it. Right now I have `jdk 16` installed. – Devorious Mar 30 '21 at 17:19
  • That just sounds like the command is not on the path. – James_D Mar 30 '21 at 17:35
  • Yes, that was the problem. I did had `jdk\bin` added in "top" PATH, but not int **System variables\PATH**. Now it did work, but when I'm trying the exe I'm getting Failed to launch JVM. – Devorious Mar 30 '21 at 17:58
  • @James_D I did found a solution to that problem as well. Thank you for your help, I'll add a complete solution and guide for this later on as an answer. Now I'll have to tinker a bit with it and see it's capabilities. – Devorious Mar 30 '21 at 18:12
  • Old thread but as of Spring 4+ (maybe before) you can create a custom JRE that includes the libraries you need. – Adam D. May 07 '23 at 02:33

2 Answers2

2

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 if jpackage 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.

Devorious
  • 107
  • 1
  • 8
1

in my case, I put javafx in the jre dir, and --module-path "jre\jafafx\lib" get the corrent libs:

this is the configure: this is my config 1

and this is the file tree: enter image description here

enter image description here

  • I found `jpackage` to be better for users too, because with that they don't have download Java (so no pop-up to confuse them) and go to the process of installing it, even tho it is easy, is better to have something that avoids it and think about a wide range of users not just some with knowledge needed for it, and it only adds like 82 MB more to the size of the app. – Devorious Oct 15 '21 at 10:51