Issue
I know you can solve this by adding
--module-path "pathToLib" --add-modules javafx.controls,javafx.fxml,javafx.graphics
when running the jar file in command line.
But my question is, Is there no permanent solution to solve this error in system settings or configurations and be able to run a jar
file as normal as java -jar myfile.jar
rather than every time I am suppose to add the module java --module-path "C:\Users\..\Downloads\javafx-sdk-17.0.2\lib" --add-modules javafx.controls,javafx.fxml,javafx.graphics -jar myfile.jar
Also I know you can make a script for this operation for every jar
file, but I was thinking of something like system settings or configuration that will be applicable for all jar
file with javafx
, and be able to run the jar file as normal as java -jar myFile.jar
I am using Ant
as a build tool.
Solution
Recommended Alternatives
See the packaging resources of the JavaFX tag for recommended alternate solutions to a jar distribution: jlink, jpackage, or native image.
Using JRE's that include JavaFX
Pre-installed JREs that include JavaFX, such as some Bellsoft, Zulu, and Corretto distributions, will execute JavaFX apps without additional module specifiers because they include the JavaFX modules in the base module setup for their distributions.
Note, you must use the correct versions of the JDKs if you want a JDK which includes JavaFX (not all JDKs include JavaFX):
- for BellSoft, download and install the "Full JDK", not the "Standard JDK".
- for Zulu, download and install the package type "JDK FX", not "JDK".
You can also create your own JRE distribution that includes JavaFX modules using jlink (which is actually simpler to do than it may sound).
Using ant to build a single JAR containing App and JavaFX components
But I still hope that there might be a solution for the above while working with ANT as building tool for JavaFX.
There is some info on building modular JavaFX apps with ant in this answer:
It probably isn’t everything you are looking for though.
To create a single executable jar using ant, you could try emulating the output of this maven JavaFX shade on classpath answer:
But use ant tasks to build the massive shaded jar instead of maven. I don’t have explicit instructions for that, you would need to work out to accomplish that non-trivial task yourself.
The created jar will include a launcher class, your application code, dependent library code, JavaFX java, and native code. The jar will run on any modern JRE as long as you have included the native code for the relevant platform. The jar will run in the unsupported classpath configuration.
Zip Distributions
Or (better) create a zip distribution:
only put your own code in your app jar.
place the dependent libraries and JavaFX modules in a lib directory.
Create a script that invokes Java with your jar file running with the modules in the lib directory added.
Make your app modular if possible:
- Define a
module-info.java
. - This step isn’t strictly necessary or reasonably possible for some apps.
- Define a
Use ant to place everything in a zip file for distribution.
Include a jlink generated JRE in the zip if you want.
Note: the maven JavaFX plugin, once properly configured, can accomplish most of these tasks with a single command:
mvn javafx:jlink
Additional info
Answered By - jewelsea
Answer Checked By - David Marino (JavaFixing Volunteer)