Starting JavaFX from Main method of class which doesn't extend Application


I'm having problem to start a JavaFX Application from a Main method of a class which doesn't extend javafx.application.Application

In my application there is the which should start the overriden method start() in the, which extends Applciation

When I start the Main method from the everything works fine.

public class MainApp {

    public static void main(String[] args) {
        PersonJDBCTemplate jdbc = connect();
        MainUIController mUIc = new MainUIController(jdbc);
        mUIc.start(new Stage());


    public static PersonJDBCTemplate connect() {
        ApplicationContext context = new ClassPathXmlApplicationContext(
        PersonJDBCTemplate personJDBCTemplate = (PersonJDBCTemplate) context
        return personJDBCTemplate;

public class MainUIController extends Application {

    private Stage stage;
    // private User loggedUser;
    private final double MINIMUM_WINDOW_WIDTH = 800.0;
    private final double MINIMUM_WINDOW_HEIGHT = 570.0;
    private String version = "0.6";
    private PersonJDBCTemplate jdbc;

    public MainUIController(PersonJDBCTemplate jdbc) {
        this.jdbc = jdbc;


    public void start(Stage primaryStage) {
        try {
            stage = primaryStage;
        } catch (Exception ex) {
                    Level.SEVERE, null, ex);

    public void gotoLogin() {
        try {
            LoginController login = (LoginController) replaceSceneContent("/fxml/Login.fxml");
        } catch (Exception ex) {
                    Level.SEVERE, null, ex);

After running the MainApp, I get the following Error :

Exception in thread "main" java.lang.ExceptionInInitializerError
at javafx.stage.Window.<init>(
at javafx.stage.Stage.<init>(
at javafx.stage.Stage.<init>(
Caused by: java.lang.IllegalStateException: This operation is permitted on the event thread only; currentThread = main
at javafx.stage.Screen.<clinit>(
... 4 more
9/5/2014 5:53:08 AM

Accepted Answer

In addition to what Nejinx said, you must not directly call your start(), always call launch(), because it sets up the JavaFX environment, including creation of stage and calls start() passing the stage as an parameter to it.

The docs has a note specially stating this

NOTE: This method is called on the JavaFX Application Thread

The launch() can be called from any class, taking into consideration if the class is directly not extending javafx.application.Application, then you must pass the class extending it as an argument to the launch method.

For example, consider you have a class JavaFXMain which extends Application

class JavaFXMain extends Application {...}

You can use any other class, to start the JavaFX Application.

class Main {
   public void someMethod() {
      JavaFXMain.launch(JavaFXMain.class); // Launch the JavaFX application

In your case, you can try something like this inside the main method of MainApp:

// You may remove args if you don't intend to pass any arguments
MainUIController.launch(MainUIController.class, args) 
3/30/2015 9:24:01 AM

You need to initialise the JavaFX environment, you cannot create a new Stage outside of launch(args); being called first on the class that extends Application.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow