Skip to content

How to launch JavaFX source code file from command line?

Since JDK 11, the java command can launch a java source code file, i.e. no need to first compile your java source code. Here is my java source code:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class HelloJavaFxWorld extends Application {

    public void start(Stage primaryStage) throws Exception {
        Label label = new Label("Hello JavaFX World");
        Pane root = new Pane(label);
        Scene scene = new Scene(root);

    public static void main(String[] args) {

I save this code as file
I open a command prompt window and enter the following command.


This is (part of) the output. error: package javafx.application does not exist
import javafx.application.Application;

How can I run a JavaFX source code file?


Since JDK 11 is modular, you need to add the JavaFX modules. Try the following.

java -p ";pathtojavafx.base.jar;pathtojavafx.controls.jar" --add-modules,javafx.controls

Replace pathto with the actual path to the JAR files. For example on my Windows 10 machine I have installed JDK 16.0.1 so I am using JavaFX 16 and have placed the [JavaFX] JAR files in this folder:

C:Program FilesJavajavafx-sdk-16lib

So my actual command for launching the JavaFX source code file is:

java -p "C:Program;C:Program FilesJavajavafx-sdk-16libjavafx.base.jar;C:Program FilesJavajavafx-sdk-16libjavafx.controls.jar" --add-modules,javafx.controls

Note that instead of -p, you can use --module-path. Then the command becomes:

java --module-path "C:Program FilesJavajavafx-sdk-16lib" --add-modules,javafx.controls

Note that I enter that command from the folder containing the java source code file.

The above command may result in the following exception.

Exception in thread "main" java.lang.RuntimeException: java.lang.ClassNotFoundException: HelloJavaFxWorld
        at HelloJavaFxWorld.main(
Caused by: java.lang.ClassNotFoundException: HelloJavaFxWorld
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(
        at java.base/java.lang.ClassLoader.loadClass(
        at java.base/java.lang.Class.forName0(Native Method)
        at java.base/java.lang.Class.forName(
        at HelloJavaFxWorld.main(
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(
        at java.base/java.lang.reflect.Method.invoke(
        at jdk.compiler/
        at jdk.compiler/
        at jdk.compiler/

This is because, when launching a java source code file, the source code is compiled and the compiled class is stored in memory, i.e. no .class file is created. Method launch(String...), in class javafx.application.Application, calls method forName, in class java.lang.Class in order to load the JavaFX application class. Since there is no HelloJavaFxWorld.class file, method forName throws ClassNotFoundException.

In order to fix that, simply change your java source code to call the other launch method. In other words, change method main to the following.

public static void main(String[] args) {
    launch(HelloJavaFxWorld.class, args);

Now, when I enter the above java command, I get the following window.

screen capture of JavaFX application window