I am running java version 11.0.2 on a Mac. WHen I compiled a java file that imports javafx.util.pair
I got an error: package javafx.util does not exist
. There are not many useful resources to solve this issue. I tried different suggestions online, but in no vain. Currently I am trying to add the JavaFX package to my Java directory. But it doesn't work.
Here is what I did:
Downloaded javafx-sdk-11.0.2 folder. Inside the folder, there are 2 directories - legal and lib
Moved items in legal
dir to /Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home/legal
Move items in lib
dir to /Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home/lib
Reopen Terminal to compile this .java file.
Question_1: Did I miss anything above?
Question_2: If this approach is wrong, what would you suggest?
javafx.util.Pair
is part of the javafx.base
module, and as you have mentioned, you need to download the JavaFX SDK to your machine, as it is no longer part of the Java JDK anymore.
You can follow the OpenJFX docs on how you can get started.
Once you have downloaded the SDK, if you want to run JavaFX 11, you will see that you need to do something as documented:
export PATH_TO_FX=path/to/javafx-sdk-11.0.2/lib
javac --module-path $PATH_TO_FX --add-modules=javafx.controls HelloFX.java
java --module-path $PATH_TO_FX --add-modules=javafx.controls HelloFX
This means that you run your Java 11 and include the modules from the JavaFX SDK independent location. Note that javafx.controls
has as transitive dependencies the javafx.base
and the javafx.graphics
modules.
However, you won't read in those docs that you have to copy the JavaFX files into the JDK. That won't work.
The main reason why it won't work: Java 11 and JavaFX 11 are modular, and even if you see the JavaFX jars under path/to/javafx-sdk-11.0.2/lib
, the JDK uses a big file: /path/to/jdk-11.0.2.jdk/Contents/Home/lib/modules
to run the java
command. That file was created when the JDK was built, therefore adding any jar to it won't have any effect.
Alternative
However, you will find in the docs the right way to "copy" the JavaFX SDK into the JDK: by creating a new custom image. See the link, section Custom JDK+JavaFX image
.
You can use
jlink
to create a runtime image that includes some or all the JavaFX modules, without being attached to a given project.
So you can create a combined image Java11+JavaFX11, and use that as your new JDK. This will allow you getting rid of the --module-path
and --add-modules
arguments (in terms of JavaFX at least).
In fact, some distributions like this one already do this.
So the options are: you use the regular JDK and the JavaFX SDK (with --module-path
and --add-modules
) or you create/use a custom JDK that includes JavaFX.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With