Java: How do I start a standalone application from the current one when both are in the same package?

You can make this work by calling start(...) directly on a new instance of one of the Application subclasses, but it kind of feels like a bit of a hack, and is contrary to the intended use of the start(...) method. (Just semantically: a method called start in a class called Application should be executed when your application starts, not at some arbitrary point after it is already running.)

You should really think of the start method as the replacement for the main method in a traditional Java application. If you had one application calling another application’s main method, you would (hopefully) come to the conclusion that you had structured things incorrectly.

So I would recommend refactoring your design so that your individual components are not application subclasses, but just plain old regular classes:

public class FirstModule {

    // can be any Parent subclass:
    private BorderPane view ;

    public FirstModule() {

        // create view; you could also just load some FXML if you use FXML
        view = new BorderPane();

        // configure view, populate with controls, etc...

    }

    public Parent getView() {
        return view ;
    }

    // other methods as needed...
}

and, similarly,

public class SecondModule {

    private GridPane view ;

    public SecondModule {

        view = new GridPane();
        // etc etc
    }

    public Parent getView() {
        return view ;
    }
}

Now you can just do things like

FirstModule firstModule = new FirstModule();
Scene scene = new Scene(firstModule.getView());
Stage stage = new Stage();
stage.setScene(scene);
stage.show();

anywhere you need to do them. So you can create standalone applications for each module:

public class FirstApplication extends Application {

    @Override
    public void start(Stage primaryStage) {
        Scene scene = new Scene(new FirstModule().getView());
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

or you can instantiate them as part of a bigger application:

public class CompositeModule {

    private HBox view ;

    public CompositeModule() {

        Button first = new Button("First Module");
        first.setOnAction(e -> {
            Parent view = new FirstModule().getView();
            Scene scene = new Scene(view);
            Stage stage = new Stage();
            stage.initOwner(first.getScene().getWindow());
            stage.setScene(scene);
            stage.show();
        });

        Button second = new Button("Second Module");
        second.setOnAction(e -> {
            Parent view = new SecondModule().getView();
            Scene scene = new Scene(view);
            Stage stage = new Stage();
            stage.initOwner(second.getScene().getWindow());
            stage.setScene(scene);
            stage.show();
        });

        HBox view = new HBox(10, first, second);
        view.setAlignment(Pos.CENTER);

    }

    public Parent getView() {
        return view ;
    }
}

and

public class CompositeApplication extends Application {
    @Override
    public void start(Stage primaryStage) {

        Scene scene = new Scene(new CompositeModule().getView(), 360, 150);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

The way I think of this is that Application subclasses represent an entire running application. Consequently it makes sense only to ever instantiate one such class once per JVM, so you should consider these inherently not to be reusable. Move any code you want to reuse into a different class somewhere.

Leave a Comment