Create a JavaFX primary stage inside a normal Java application


I have a launcher and a JavaFX class. The launcher creates a class called JavaFXApplication1. The JavaFXApplication contains the whole JavaFX code (Just a little example in this case) and should setup a window with one primary stage.

The launcher has the static main entry point - but I read that JavaFX doesn't really use this entry point. This explains my console output (See the end of the post)

I don't know if this is possible (Launcher create a JavaFX window - the entry point is not in the presentation class itself) . I don't want to use a preloader (I think preloaders are just for heavy loads during startup), because the launcher represents the whole program as one object (Presentation, business and persistence - a 3 layer program). The entry point should be outside the presentation class (in this example in the launcher class)

The following example does work. But for me it is like a piece of "black magic"

Here is my code


package javafxapplication1;

public class Launcher
    public static void main(String[] args)

        // Do some stuff and then create the UI class

        JavaFXApplication1 client = new JavaFXApplication1();


package javafxapplication1;

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

public class JavaFXApplication1 extends Application
    public void start(Stage primaryStage)
        primaryStage.setTitle("I am a JavaFX app");;

    public void caller(String[] args)
    /* We call the main function from the client
    public static void main(String[] args)

And the output for the program is:


Is there a way to create such an application ? Thank you

2/11/2013 6:45:14 PM

Accepted Answer

The answer to this problem is to create a java project and not a JavaFX project. After this you can add a JavaFX main class and write a method (call launch() ).

Maybe you have to add the compile-time libraries deploy.jar, javaws.jar, jfxrt.jar and plugin.jar from the /jdk_*/jre/lib directory

2/13/2013 8:00:29 AM

I written a post at Running JavaFX Application instance in the main method of a class – MacDevign

Could it be what you looking for ?

The code is quite long hence it is better to refer to the post however the usage is simple. Take note that the init and stop method do not use launcher thread so use it with care.

The purpose is to run a dummy javafx application on your main method of your class for quick testing/experimenting purpose.

To use this, just add the following in the main method using lambda, or alternatively you can use anonymous inner class style.

// using the start method of Application class
Utility.launchApp((app, stage) -> {
    // javafx code
}, arArgs);

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