Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

e.g.

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. However when I 'run' this code, no windows open. Instead an application called 'java' opens. It appears 'java' is simply a 'unix executable file' located in 'jdk1.8.0_25.jdk/contents/home/bin'. The application 'java' displays absolutely nothing, and cannot be shut down without force quitting.

I'm running eclipse on a Macbook. 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.

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.

like image 276
Joe Morgan Avatar asked Dec 09 '22 05:12

Joe Morgan


2 Answers

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".

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.

I hope this information is able to help someone.

like image 194
heyitschrislol Avatar answered Dec 11 '22 12:12

heyitschrislol


possible solution install this from the eclipse marketplace https://www.eclipse.org/efxclipse/install.html

like image 38
Mark Avatar answered Dec 11 '22 12:12

Mark