4 Compiling and Running a JavaFX Application

The following steps show you how to compile and run a simple JavaFX application.

  1. Create the following directory tree:

    • myapplication
      • src
        • helloworld
      • bin

    Run all the commands in these steps in the directory myapplication.

  2. 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();
        }
    }
  3. Add an environment variable pointing to the lib directory 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"
  4. Compile HelloWorld.java. In the --module-path command-line option, specify the lib directory of the JavaFX SDK. In the --add-modules command-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
  5. 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-access to suppress this warning.

    See Restricted Methods in Java Platform, Standard Edition Core Libraries for more information about restricted methods and the --enable-native-access option.

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.

  1. In the directory myapplication, create a subdirectory named mods.

  2. 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"
  3. Save the following code in a file named src/module-info.java

    module helloworld {
        requires javafx.controls;
        exports helloworld;
    }
  4. Compile the HelloWorldFX application 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
  5. Test the HelloWorldFX modular 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
  6. Create a custom JRE for the HelloWorldFX modular application with jlink:

    • 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
  7. Run HelloWorldFX with 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