今天如何在 Java 8 Ubuntu 上启动 JavaFX 应用程序?

nll*_*dfx 5 java javafx javafx-8

我试图启动应用程序但出现错误:

java.lang.ClassNotFoundException: com.sun.glass.ui.gtk.GtkPlatformFactory   at
  java.net.URLClassLoader.findClass(URLClassLoader.java:381)  at
  java.lang.ClassLoader.loadClass(ClassLoader.java:424)   at
  java.lang.ClassLoader.loadClass(ClassLoader.java:357)   at
  java.lang.Class.forName0(Native Method)   at
  java.lang.Class.forName(Class.java:264)   at
  com.sun.glass.ui.PlatformFactory.getPlatformFactory(PlatformFactory.java:42)
    at com.sun.glass.ui.Application.run(Application.java:146)   at
  com.sun.javafx.tk.quantum.QuantumToolkit.startup(QuantumToolkit.java:257)
    at
  com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:211)
    at
  com.sun.javafx.application.LauncherImpl.startToolkit(LauncherImpl.java:675)
    at
  com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:337)
    at
  com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  at
  sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at
  sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)   at
  sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
  Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  at
  sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at
  sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)   at
  sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
  Caused by: java.lang.NullPointerException   at
  com.sun.glass.ui.Application.run(Application.java:146)  at
  com.sun.javafx.tk.quantum.QuantumToolkit.startup(QuantumToolkit.java:257)
    at
  com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:211)
    at
  com.sun.javafx.application.LauncherImpl.startToolkit(LauncherImpl.java:675)
    at
  com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:337)
    at
  com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    ... 5 more 
Run Code Online (Sandbox Code Playgroud)

以及Failed to load Glass factory class堆栈跟踪末尾的消息。

我还尝试将 javafx 添加为 maven 依赖项并将此 jar 添加到类路径。我在我的项目中使用 IDEA。

更新

我的代码很简单 Hello JavaFX 示例:

public class Application extends javafx.application.Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });

        StackPane root = new StackPane();
        root.getChildren().add(btn);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
Run Code Online (Sandbox Code Playgroud)

更新1:

我使用 debug 并在 ThreadPoolExecutor.java 中的第 1057 行发现了 excpetion throws。另外:

在此处输入图片说明

是的!执行此代码片段后,项目控制台中出现第一个异常。

nll*_*dfx 0

好吧,我明白了。

首先我的jdk坏了所以我下载了新的。

另外,在 IDEA 中,即使您在“项目”选项卡中更改了 sdk,也不要忘记在“模块”部分中更改项目 sdk。