How do I launch JavaFX applications on Java 8 Ubuntu?

I tried to start the application with an error:

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

And the message cannot load the glass factory class at the end of the stack trace

I also tried to add JavaFX as a maven dependency and add this jar to the classpath I use idea for my project

to update:

My code is simple hello JavaFX exmaple:

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();
    }
}

Update 1:

I used debug and found ThreadPoolExecutor. On line 1057 Excsession in Java is thrown In addition:

yes! After executing this code snippet, the first exception appears in the project console

Solution

You need to download and install openjfx lib

Sudo apt install openjfx should solve the problem

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>