1

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.

Gabriel Rogath
  • 710
  • 2
  • 8
  • 23
  • 2
    See the [packaging resources of the JavaFX tag](https://stackoverflow.com/tags/javafx/info) for alternate solutions, e.g. jlink, jpackage, native image. – jewelsea Mar 10 '22 at 20:03
  • 2
    Off-topic, but IMO, other tools such as Maven and Gradle are preferable to ant for building JavaFX applications. – jewelsea Mar 10 '22 at 22:15
  • Thanks @jewelsea, Indeed your comments have given me more insight and alternatives on how to deal with this. But I still hope that there might be a solution for the above while working with `ANT` as building tool for `JavaFX`. Thanks once again – Gabriel Rogath Mar 11 '22 at 03:57

2 Answers2

3

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:

  1. only put your own code in your app jar.

  2. place the dependent libraries and JavaFX modules in a lib directory.

  3. Create a script that invokes Java with your jar file running with the modules in the lib directory added.

  4. Make your app modular if possible:

    • Define a module-info.java.
    • This step isn’t strictly necessary or reasonably possible for some apps.
  5. Use ant to place everything in a zip file for distribution.

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

jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • I uninstalled my JDK and started installed JDK from [bellsoft](https://bell-sw.com/pages/liberica-release-notes-17.0.2/) with a release note that **Java FX Graphics - works**. **Java FX Controls - works**. **Java FX Media - does not work**. **Java FX Webkit - does not work**. On the IDE IntelJ it works fine without adding library but after generating the jar file it does not work until you add module external as above in command line. Likewise with Zulu it behaves as Bellsoft. Thanks in advance @jewelsea – Gabriel Rogath Mar 12 '22 at 07:00
  • 2
    Are you using the correct Java version on the command line? Check it with `java -v`. It should report Zulu or Bellsoft. Does your jar contain any JavaFX framework code? (It should not for this config). Run `java --list-modules`, you should see the JavaFX modules listed. – jewelsea Mar 12 '22 at 07:10
  • 2
    That release note you mention is for the liberica jdk for embedded version for the raspberry pi, which you shouldn’t be using. Unless you are using a pi, which you aren’t or I am sure you would have said that. The liberica full jdk version for any of the major OSes should have working versions of all JavaFX modules as far as I can tell (I haven’t tried using that distribution in a while, so cannot confirm myself at this time). – jewelsea Mar 12 '22 at 07:25
  • 2
    Ensure you have correctly followed an [installation guide](https://bell-sw.com/pages/liberica_install_guide-17.0.2/) for a JDK version for your OS. And made sure that the version includes JavaFX. – jewelsea Mar 12 '22 at 07:34
  • `C:\>java -version openjdk version "17.0.2" 2022-01-18 LTS OpenJDK Runtime Environment (build 17.0.2+9-LTS) OpenJDK 64-Bit Server VM (build 17.0.2+9-LTS, mixed mode, sharing)` . I downloded this for [window](https://download.bell-sw.com/java/17.0.2+9/bellsoft-jdk17.0.2+9-windows-amd64.msi), and if I check `java --list-modules` I don't see javaFx module – Gabriel Rogath Mar 12 '22 at 07:35
  • 1
    That is the wrong download link, you need to use the liberica full jdk not the standard JDK. Only the bell soft full JDK distributions include JavaFX, as their documentation states. The link to the full JDK windows installer is: https://download.bell-sw.com/java/17.0.2+9/bellsoft-jdk17.0.2+9-windows-amd64-full.msi – jewelsea Mar 12 '22 at 07:47
  • I uninstalled that one and installed from [zulu](https://www.azul.com/downloads/?package=jdk). `C:\>java -version` _openjdk version "17.0.2" 2022-01-18 LTS OpenJDK Runtime Environment Zulu17.32+13-CA (build 17.0.2+8-LTS) OpenJDK 64-Bit Server VM Zulu17.32+13-CA (build 17.0.2+8-LTS, mixed mode, sharing)_ . Both seems to be OpenJDK – Gabriel Rogath Mar 12 '22 at 07:49
  • 1
    Once it is installed you should have it on your path, and have the JAVA_HOME environment variable set to its location and you should not have open jdk on your path (safest just to remove the OpenJDK installation from your PC if you don't use it). – jewelsea Mar 12 '22 at 07:49
  • Hah,,Let me get that one and see. Thanks @jewelsea – Gabriel Rogath Mar 12 '22 at 07:51
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/242876/discussion-between-jewelsea-and-gabriel-rogath). – jewelsea Mar 12 '22 at 07:52
1

Add a module-info.java file under your java/ folder and populate it with the following content:

module module_name {
requires javafx.controls;
requires javafx.fxml;
requires javafx.graphics;
requires java.base;
requires java.desktop;

opens com.example.matformater to javafx.graphics;
opens com.example.matformater.controller to javafx.fxml;}