How do I start again an external JavaFX program? Launch prevents this, even if the JavaFX program ended with Platform.Exit



From my MainProject (Java 8) i starts a JavaFX 8 Class.

public void startFX() {
    if (isRestartPrintModul() == true) {
    } else {
        fxMain.main(new String[] {"ohne"});

This is my FXMain:

        package quality;

    import javafx.application.Application;
    import javafx.application.Platform;
    import javafx.event.ActionEvent;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Modality;
    import javafx.stage.Stage;
    import javafx.stage.WindowEvent;

     * @author pu_laib
    public class FXMain extends Application {

        private static Stage primaryStage;

        public void init() {
            if (getPrimaryStage() != null) {
            } else {

        public void start(Stage primaryStage) {
            // -> Applicationerror: getPrimaryStage().initModality(Modality.NONE);
            // -> Applicationerror: getPrimaryStage().initModality(Modality.APPLICATION_MODAL);
            Button btn = new Button();
            btn.setText("Say 'Hello World'");
            btn.setOnAction((ActionEvent event) -> {
                System.out.println("Hello World!");

            StackPane root = new StackPane();

            Scene scene = new Scene(root, 300, 250);

            getPrimaryStage().setTitle("Hello World!");
            this.primaryStage.setOnCloseRequest((WindowEvent event) -> {


        public static void main(String[] args) {

        public Stage getPrimaryStage() {
            return primaryStage;

        public void setPrimaryStage(Stage primaryStage) {
            this.primaryStage = primaryStage;


It is not possible to call the Print module from my MainProject again, although it is closed in my opinion.

Once the PrintModul module is completed, launch can not remember that it ran before, right?

What is wrong?

Thank you.





The documentation for the Application::launch(args) method states:

It must not be called more than once or an exception will be thrown.


  1. Just call launch once.
  2. Invoke Platform.setImplicitExit(false) so that the JavaFX runtime will continue running even if all stages are closed.
  3. Once the JavaFX application has finished doing its job, don’t invoke Platform.exit(), but let the JavaFX platform continue running even when you aren’t actively using it.
  4. Instead of trying to launch the application again, invoke the start() method of the application (or some other public method you provide on the application which accepts the parameters you want to pass) to “run” the application a second or more times (perhaps instantiating a new stage to pass to start method if needed).
  5. Once all of the work is complete, invoke Platform.exit() at that time to cleanly shutdown the JavaFX system.

Your other option would be to launch a new process instead of running the JavaFX applications inside the same process as your MainProject, but, in general, I’d recommend the approach outlined above rather than creating new processes.


Facebook Comments

Post a comment