5

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.

Joe Morgan
  • 430
  • 2
  • 8
  • 19
  • '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`. – nitind Jan 08 '15 at 19:33
  • it's the code taken from the oracle website http://docs.oracle.com/javafx/2/get_started/hello_world.htm is it incorrect? – Joe Morgan Jan 08 '15 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). – James_D Jan 08 '15 at 19:48
  • so have you any idea what I am doing wrong? or is my eclipse set up incorrectly? – Joe Morgan Jan 08 '15 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.) – James_D Jan 08 '15 at 19:54
  • java version "1.8.0_25" - Joes-MBP-2:~ JoeMorgan$ java cp ~/Documents/workspace/MenuTest/bin/test test.Application Error: Could not find or load main class cp – Joe Morgan Jan 08 '15 at 19:59
  • to specify command line arguments to the java runtime, prefix them with a minus sign, for example `java -cp`. However in your case you don't need to use a class path, instead change the current directory to ` ~/Documents/workspace/MenuTest/bin` and issue the command `java test.Application`. For more info see: [Compile code using JavaFX 2.0 (using command line)](http://stackoverflow.com/questions/9436219/compile-code-using-javafx-2-0-using-command-line). Also try this [e(fx)clipse tutorial](http://wiki.eclipse.org/Efxclipse/Tutorials/Tutorial1). – jewelsea Jan 08 '15 at 20:06
  • Joes-MBP-2:~ JoeMorgan$ cd ~/Documents/workspace/MenuTest/bin Joes-MBP-2:bin JoeMorgan$ java test.Application Error: Could not find or load main class test.Application – Joe Morgan Jan 08 '15 at 20:07
  • test.application does not exist in that bin folder, only test.class... – Joe Morgan Jan 08 '15 at 20:08
  • Since both your package name and your class name are "test", you should have a "test" directory containing "test.class". (Following the standard naming conventions - packages all lower case and class name beginning uppercase - will help here, however...) From the *parent* of the "test" *directory* (it should be `bin`), run `java test.test`. – James_D Jan 08 '15 at 20:28
  • I was using lowercase just to make a quick example. Joes-MBP-2:test JoeMorgan$ cd ~/Documents/workspace/MenuTest/bin/ Joes-MBP-2:bin JoeMorgan$ java test.test Error: Could not find or load main class test.test – Joe Morgan Jan 08 '15 at 20:36
  • Inside `bin` is a `test` folder, and inside that is `test.class`? – James_D Jan 08 '15 at 20:38
  • okay it appears that the folder was incorrectly named. now when I run the command Joes-MBP-2:bin JoeMorgan$ java test.test the application loads as it should. So i guess i'm getting somewhere. However it does not run eclipse – Joe Morgan Jan 08 '15 at 20:41

5 Answers5

15

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.

heyitschrislol
  • 151
  • 1
  • 3
1

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

Mark
  • 1,337
  • 23
  • 34
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.

Quoshak
  • 11
  • 3
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

rpetruzz
  • 107
  • 1
  • 10
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.

Jongbo Im
  • 23
  • 4