Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX Installation

I am new to Java and currently reading through a text and learning. I have installed the JDK in my /opt directory (working on Kubuntu). I have also installed openjfx in /opt. Since I work out the terminal, whenever I want to compile or run something involving a module from JavaFX, I use

javac --module-path $PATH_TO_FX --add-modules ALL-MODULE-PATH className.java

However, when I try the same thing with jshell it will not work. That is,

jshell --module-path $PATH_TO_FX --add-modules ALL-MODULE-PATH

What I am trying to find out is, is there any way I can install the JavaFX SDK so that it is merged with the Java SDK directory and saves me having to use the flags whenever I want to compile/run a program that requires any modules from JavaFX?

If not, can anyone tell me why the jshell comand above will not work as I expect it to?

like image 450
Zeta-Squared Avatar asked Feb 02 '26 21:02

Zeta-Squared


1 Answers

For this part of your question:

What I am trying to find out is, is there any way I can install the JavaFX SDK so that it is merged with the Java SDK directory and saves me having to use the flags whenever I want to compile/run a program that requires any modules from JavaFX?

you can use jlink, which generates a custom runtime image. In order to include services in the generated runtime image, use the --bind-services option. For reasons I don't quite understand, to include jshell in this, you must explicitly add the jdk.jshell module (simply using --add-modules ALL-MODULE-PATH will not work). So you need something like:

jlink --module-path $PATH_TO_FX --add-modules javafx.controls,javafx.fxml,jdk.jshell --bind-services --output JavaWithFX

to create a JDK that includes the JavaFX modules. You might need to include other JavaFX modules (e.g. javafx.web if you are using WebView, javafx.media if you are using MediaPlayer, etc.), or omit javafx.fxml if you are not using FXML.

Now you can just use that JDK to compile and run:

export JAVA_HOME=JavaWithFX
javac ClassName.java
java ClassName

(or possibly export PATH=JavaWithFX/bin:$PATH instead of the export JAVA_HOME, depending on how Java works on your system).

Running

JavaWithFX/bin/jshell

will give you a jshell that includes the JavaFX modules:

% JavaWithFX/bin/jshell
|  Welcome to JShell -- Version 20
|  For an introduction type: /help intro

jshell> import javafx.beans.property.SimpleIntegerProperty

jshell> var x = new SimpleIntegerProperty(0)
x ==> IntegerProperty [value: 0]

jshell> x.addListener((obs, oldValue, newValue) -> System.out.printf("x changed from %d to %d%n", oldValue, newValue))

jshell> x.set(42)
x changed from 0 to 42

jshell> 
like image 83
James_D Avatar answered Feb 05 '26 12:02

James_D



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!