简体   繁体   English

Eclipse 未运行 JavaFX 应用程序 - 运行“java”应用程序

[英]Eclipse not running JavaFX application - runs 'java' application

Whenever I create a project in Eclipse and include javafx, the application does not load when I click the run button.每当我在 Eclipse 中创建一个项目并包含 javafx 时,当我单击运行按钮时应用程序不会加载。

eg例如

package test;

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

This should run a simple hello world application, taken from the oracle documentation.这应该运行一个简单的 hello world 应用程序,该应用程序取自 oracle 文档。 However when I 'run' this code, no windows open.但是,当我“运行”此代码时,没有打开 windows。 Instead an application called 'java' opens.取而代之的是打开一个名为“java”的应用程序。 It appears 'java' is simply a 'unix executable file' located in 'jdk1.8.0_25.jdk/contents/home/bin'.看来“java”只是位于“jdk1.8.0_25.jdk/contents/home/bin”中的“unix 可执行文件”。 The application 'java' displays absolutely nothing, and cannot be shut down without force quitting.应用程序“java”完全不显示任何内容,并且在不强制退出的情况下无法关闭。

I'm running eclipse on a Macbook.我在 Macbook 上运行 eclipse。 I've probably left out some important details...我可能遗漏了一些重要的细节......

Does anyone know why my application is not running as it should?有谁知道为什么我的应用程序没有正常运行? Forgive my naivety, I'm new to java and eclipse.原谅我的天真,我是 java 和 eclipse 的新手。

Many Thanks非常感谢

EDIT:编辑:

import javafx.application.Application;
import javafx.stage.Stage;


public class JavaFX extends Application {

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

    @Override
    public void start(Stage stage) throws Exception {
        // TODO Auto-generated method stub
        stage.show();

    }

}

This simple program also gives the same error.这个简单的程序也给出了同样的错误。

So I know this is an old question, but I ran into what I believe to be the same issue recently and wanted to share the solution I found (although I have no insight as to why it works).所以我知道这是一个老问题,但我最近遇到了我认为是同一个问题并想分享我找到的解决方案(尽管我不知道为什么起作用)。

pictured如图

Go to Run Configurations for the main class, and on the "Arguments" tab, uncheck the box where it says "Use the -XstartOnFirstThread argument when launching with SWT".转到主类的运行配置,然后在“参数”选项卡上,取消选中“使用 SWT 启动时使用 -XstartOnFirstThread 参数”的框。

To add a visual example of the problem so that someone more knowledgable than I can possibly explain why this happens/why this solution works:添加问题的视觉示例,以便比我更有见识的人可以解释为什么会发生这种情况/为什么这个解决方案有效:

This is what you get when you try to run the program.就是您尝试运行程序时得到的结果。 An application simply called "java" appears to be running, but nothing is showing.一个名为“java”的应用程序似乎正在运行,但没有显示任何内容。

I hope this information is able to help someone.我希望这些信息能够帮助某人。

可能的解决方案从 eclipse 市场https://www.eclipse.org/efxclipse/install.html安装它

I've had the same problem when I was trying to run JavaFX main class in an existing SWT project with maven dependency:当我尝试在具有 maven 依赖项的现有 SWT 项目中运行 JavaFX 主类时遇到了同样的问题:

<dependency>
   <groupId>org.eclipse.swt</groupId>
    <artifactId>org.eclipse.swt.cocoa.macosx.x86_64</artifactId>
    <version>4.3</version>
</dependency>

When I comment out this dependency, application window was shown and everything worked well.当我注释掉这个依赖项时,显示了应用程序窗口并且一切正常。

Just to let you know, I created a new project in Eclipse for Java and copy / pasted your code into it.只是为了让您知道,我在 Eclipse 中为 Java 创建了一个新项目,并将您的代码复制/粘贴到其中。 Then just simply clicked on the run icon.然后只需单击运行图标即可。 It worked perfectly.它工作得很好。 I'm going to attach screen grabs of my setup for you.我将为您附上我的设置的屏幕截图。

初始启动

运行配置 Main

运行配置参数

运行配置命令行

if the pane is not showing up go to run configuration by clicking little arrow by run button and go to argument tab and uncheck the -use the -xstartonfirstthtread box then run again.如果窗格未显示 go 通过单击运行按钮和 go 到参数选项卡并取消选中 - 使用 -xstartonfirstthtread 框来运行配置,然后再次运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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