4 Compiling and Running a JavaFX Application
The following steps show you how to compile and run a simple JavaFX application.
-
Create the following directory tree:
myapplicationsrchelloworld
bin
Run all the commands in these steps in the directory
myapplication. -
Save the following code in a file named
src/helloworld/HelloWorldFX.java:package helloworld; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.StackPane; import javafx.stage.Stage; public class HelloWorldFX extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("Hello World!"); Button btn = new Button(); btn.setText("Say 'Hello World'"); btn.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { System.out.println("Hello World!"); } }); StackPane root = new StackPane(); root.getChildren().add(btn); primaryStage.setScene(new Scene(root, 300, 250)); primaryStage.show(); } } -
Add an environment variable pointing to the
libdirectory of the JavaFX SDK. For example:- Linux and macOS:
export JAVAFX_SDK=/path/to/javafx_sdk/lib - Windows:
set JAVAFX_SDK="C:\path\to\javafx_sdk\lib"
- Linux and macOS:
-
Compile
HelloWorld.java. In the--module-pathcommand-line option, specify thelibdirectory of the JavaFX SDK. In the--add-modulescommand-line option, specify the JavaFX modules that your application uses. For example:- Linux and macOS:
javac --module-path $JAVAFX_SDK --add-modules javafx.controls \ -d bin HelloWorld.java - Windows:
javac --module-path %JAVAFX_SDK% --add-modules javafx.controls ^ -d bin src/helloworld/HelloWorldFX.java
- Linux and macOS:
-
Run the application.
- Linux and macOS:
java --module-path $JAVAFX_SDK --add-modules javafx.controls \ --enable-native-access=javafx.graphics -cp bin helloworld.HelloWorldFX - Windows:
java --module-path %JAVAFX_SDK% --add-modules javafx.controls ^ --enable-native-access=javafx.graphics -cp bin helloworld.HelloWorldFX
Note:
Some JavaFX modules, such as javafx.graphics call restricted methods, which causes the Java runtime to print a warning message. This example uses the command-line option--enable-native-accessto suppress this warning.See Restricted Methods in Java Platform, Standard Edition Core Libraries for more information about restricted methods and the
--enable-native-accessoption. - Linux and macOS:
Creating a Custom JRE for a JavaFX Application
You can also download JavaFX JMOD files, which you can use to create a custom runtime image for your JavaFX application.
These steps show you how to create a custom JRE for the HelloWorldFX sample application described in Compiling and Running a JavaFX Application. Run all the commands from the myapplication directory.
-
In the directory
myapplication, create a subdirectory namedmods. -
Add an environment variable pointing to the directory that contains the JavaFX JMOD files. For example:
- Linux and macOS:
export JAVAFX_JMODS=/path/to/javafx_jmods - Windows:
set JAVAFX_JMODS="C:\path\to\javafx_jmods"
- Linux and macOS:
-
Save the following code in a file named
src/module-info.javamodule helloworld { requires javafx.controls; exports helloworld; } -
Compile the
HelloWorldFXapplication as a module:- Linux and macOS
javac --module-path $JAVAFX_MODS -d mods/helloworld \ src/module-info.java src/helloworld/HelloWorldFX.java - Windows
javac --module-path %JAVAFX_MODS% -d mods\helloworld ^ src\module-info.java src\helloworld/HelloWorldFX.java
Note:
To compile all Java source files in a directory, use the following command:- Linux and macOS
javac --module-path $JAVAFX_MODS -d mods/helloworld $(find src/ -name "*.java") - Windows
dir /s /b src\*.java > source-files.txt & javac --module-path %JAVAFX_MODS% ^ -d mods/helloworld @source-files.txt & del source-files.txt
- Linux and macOS
-
Test the
HelloWorldFXmodular application by running it:- Linux and macOS
java --module-path $JAVAFX_SDK:mods --enable-native-access=javafx.graphics \ -m helloworld/helloworld.HelloWorldFX - Windows
java --module-path "%JAVAFX_SDK%;mods" --enable-native-access=javafx.graphics ^ -m helloworld/helloworld.HelloWorldFX
- Linux and macOS
-
Create a custom JRE for the
HelloWorldFXmodular application withjlink:- Linux and macOS
jlink --module-path %JAVAFX_MODS:mods --add-modules helloworld --output helloworld - Windows
jlink --module-path "%JAVAFX_MODS%;mods" --add-modules helloworld --output helloworld
- Linux and macOS
-
Run
HelloWorldFXwith the custom JRE you just created:- Linux and macOS
helloworld/bin/java --enable-native-access=javafx.graphics -m helloworld/helloworld.HelloWorldFX - Windows
helloworld\bin\java --enable-native-access=javafx.graphics -m helloworld/helloworld.HelloWorldFX
- Linux and macOS