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 {
@Override
public void start(Stage primaryStage) throws Exception {
Label label = new Label("Hello JavaFX World");
Pane root = new Pane(label);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I save this code as file HelloJavaFxWorld.java
.
I open a command prompt window and enter the following command.
java HelloJavaFxWorld.java
This is (part of) the output.
HelloJavaFxWorld.java:1: error: package javafx.application does not exist
import javafx.application.Application;
How can I run a JavaFX source code file?
Advertisement
Answer
Since JDK 11 is modular, you need to add the JavaFX modules. Try the following.
java -p "pathtojavafx.graphics.jar;pathtojavafx.base.jar;pathtojavafx.controls.jar" --add-modules javafx.graphics,javafx.controls HelloJavaFxWorld.java
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 FilesJavajavafx-sdk-16libjavafx.graphics.jar;C:Program FilesJavajavafx-sdk-16libjavafx.base.jar;C:Program FilesJavajavafx-sdk-16libjavafx.controls.jar" --add-modules javafx.graphics,javafx.controls HelloJavaFxWorld.java
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.graphics,javafx.controls HelloJavaFxWorld.java
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 javafx.graphics/javafx.application.Application.launch(Application.java:310)
at HelloJavaFxWorld.main(HelloJavaFxWorld.java:19)
Caused by: java.lang.ClassNotFoundException: HelloJavaFxWorld
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:636)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:182)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:519)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:466)
at javafx.graphics/javafx.application.Application.launch(Application.java:298)
at HelloJavaFxWorld.main(HelloJavaFxWorld.java:19)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at jdk.compiler/com.sun.tools.javac.launcher.Main.execute(Main.java:415)
at jdk.compiler/com.sun.tools.javac.launcher.Main.run(Main.java:192)
at jdk.compiler/com.sun.tools.javac.launcher.Main.main(Main.java:132)
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.