Cannot launch a JavaFX Application on MacOS (using Java 8)

怎甘沉沦 提交于 2021-02-19 05:25:05

问题


First time using JavaFX, I'm trying to test it with an Hello World! window.

I'm using Java 8 (update 211) with Eclipse on a MacOS with Mojave 10.14.5.

I'm working on this project:

image eclipse

I put all the JavaFX jar I think I need into the build path (maybe I'm missing some jars?).

I'm trying to open a window with JavaFX using this code:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Test extends Application{
    public static void main(final String[] args) {
        launch(args);
    }

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

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

        StackPane root = new StackPane();
        root.getChildren().add(btn);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }
}

The code should be fine, as it works on Windows and Linux.

When I run it, a java items appear in the lower bar, as you can see here:

image menu bar

I know it's opened because there is the little dot beneath it, but I can't see any window. Moreover, I can't manage to close this "cup of coffee" item without closing eclipse itself.

EDIT: I'm running it from the IDE (Eclipse 4.9.0). using Java 8 (211) from Oracle. I added the referenced libraries myself if I try to remove them leaving the imports in the code, I get this error:

Access restriction: The type 'Application' is not API (restriction on required library '/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/ext/jfxrt.jar')

回答1:


Problem solved! The problem was with the package: javafx-swt.jar

In the Eclipse IDE for Mac, when a Java program references SWT, the IDE automatically adds the VM option -XstartOnFirstThread. In most cases, this automatic addition is helpful. However, there is one case when adding this VM option causes a problem, namely with an Eclipse project for an SWT application that also includes one or more "pure" JavaFX classes that do not interoperate with the SWT classes. A "pure" JavaFX application that is launched from such an Eclipse project will hang because it does not expect -XstartOnFirstThread. (https://docs.oracle.com/javafx/2/swt_interoperability/jfxpub-swt_interoperability.htm)

In the Run configurations, Arguments tab you can find a checkbox to ask not to use the -XstartOnFirstThread. Unchecking it, it works.




回答2:


Your configuration will not work this way. You are not giving enough details to give you a definitive answer but here are some issues:

Java8 from Oracle (for the Mac) does already include JavaFX so the libraries you have added are either just not needed or, more likely, are confusing the VM.

If your Java is an OpenJDK 8 which does not include JavaFX then the libraries you have added are useless too because they look like the ones for Java 11 or later and are not compatible with Java 8.

Have a look here to find the correct setup for your environment: https://openjfx.io/openjfx-docs/

Add these two lines of code to your main method in order to find out which version of Java and JavaFX you are actually using at runtime.

System.out.println("java.runtime.version: " + System.getProperty("java.runtime.version", "(undefined)"));
System.out.println("javafx.version: " + System.getProperty("javafx.version", "(undefined)"));

The output should read something like this:

java.runtime.version: 1.8.0_212-b10
javafx.version: 8.0.212


来源:https://stackoverflow.com/questions/56776190/cannot-launch-a-javafx-application-on-macos-using-java-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!