Run HelloWorld using JavaFX SDK

If you want to use the JavaFX SDK instead of a build tool, download an appropriate JavaFX runtime for your operating system and unzip it to a desired location. For this tutorial, we will be using JavaFX 11.

Add an environment variable pointing to the lib directory of the runtime:


export PATH_TO_FX=path/to/javafx-sdk-11/lib

set PATH_TO_FX="path\to\javafx-sdk-11\lib"

You can now compile and run JavaFX applications from the command line using the JavaFX runtime.

Compile the application (e.g. use HelloFX.java from this sample) using:


javac --module-path $PATH_TO_FX --add-modules javafx.controls HelloFX.java

javac --module-path %PATH_TO_FX% --add-modules javafx.controls HelloFX.java

Important: Make sure to add the required modules, keeping into account transitive dependencies are automatically resolved (for instance, there is no need to add javafx.graphics module, since it is transitively required by the javafx.controls module). But if your application is using FXML, you will need to add the javafx.fxml module as well, as shown below:


javac --module-path $PATH_TO_FX --add-modules javafx.controls,javafx.fxml HelloFX.java

javac --module-path %PATH_TO_FX% --add-modules javafx.controls,javafx.fxml HelloFX.java

Finally, run the application using:


java --module-path $PATH_TO_FX --add-modules javafx.controls HelloFX

java --module-path %PATH_TO_FX% --add-modules javafx.controls HelloFX