简体   繁体   中英

How to launch JavaFX source code file from command line?

Since JDK 11, the java command can launch a java source code file, ie 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?

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

java -p "path\to\javafx.graphics.jar;path\to\javafx.base.jar;path\to\javafx.controls.jar" --add-modules javafx.graphics,javafx.controls HelloJavaFxWorld.java

Replace path\to 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 Files\Java\javafx-sdk-16\lib

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

java -p "C:\Program Files\Java\javafx-sdk-16\lib\javafx.graphics.jar;C:\Program Files\Java\javafx-sdk-16\lib\javafx.base.jar;C:\Program Files\Java\javafx-sdk-16\lib\javafx.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 Files\Java\javafx-sdk-16\lib" --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, ie 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.

JavaFX 应用程序窗口的屏幕截图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM