6

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.

13
  • 'java' is the executable for the JVM, inside of which your Java/JavaFX application executes. You're showing the implementation of start but apparently calling a method named launch. Commented Jan 8, 2015 at 19:33
  • it's the code taken from the oracle website docs.oracle.com/javafx/2/get_started/hello_world.htm is it incorrect? Commented Jan 8, 2015 at 19:36
  • 1
    No, the code is correct. @nitind this is the correct way to launch a JavaFX application. (The static launch() method is inherited from Application.) This runs fine for me (on Mac OS X 10.9.5). Commented Jan 8, 2015 at 19:48
  • so have you any idea what I am doing wrong? or is my eclipse set up incorrectly? Commented Jan 8, 2015 at 19:50
  • Try running from the Terminal? First check your Java version with java -version and then try java -cp /path/to/project/bin test.test. (/path/to/project should be something like ~/Documents/workspace/projectName and the bin subdirectory should contain test/test.class. Folders etc may very depending on your Eclipse setup.) Commented Jan 8, 2015 at 19:54

5 Answers 5

16

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This is a great answer (+1), but a deeper explanation would be more helpful (if anyone more knowledgable can pitch in)
1

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

1 Comment

No, e(fx)clipse is not essential to run JavaFX application from within Eclipse.
0

I've had the same problem when I was trying to run JavaFX main class in an existing SWT project with maven dependency:

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

Comments

0

Just to let you know, I created a new project in Eclipse for Java and copy / pasted your code into it. Then just simply clicked on the run icon. It worked perfectly. I'm going to attach screen grabs of my setup for you.

Initial Start up

Run configuration Main

Run configuration Arguments

Run configuration command line

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.