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

3

1

This seems like it should be easy, so I must be missing something obvious: I have 4 standalone applications in the same package, us.glenedwards.myPackage,

  • myClass1 extends Application
  • myClass2 extends Application

etc...

I need each class to act as its own standalone application. Yet I want to be able to start the other 3 classes from the one I'm in by clicking a link. Android allows me to do this using Intents:

Intent intent = new Intent(this, EditData.class);
overridePendingTransition(R.layout.edit_data_scrollview, R.layout.state);
startActivity(intent);

I've tried starting myClass2 from myClass1 using

myClass2.launch("");

But I get an error, "Application launch must not be called more than once". The only way I can get it to work is if I remove both "extends application" and the start() method from myClass2, which means that myClass2 is no longer a standalone application.

How can I start myClass2, myClass3, or myClass4 from myClass1 with all 4 of them being standalone applications?

Chemotaxis answered 8/9, 2015 at 18:21 Comment(0)
K
4

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.

Kelcey answered 8/9, 2015 at 19:41 Comment(1)
That was well thought out and well organized. I can do all I need to do from one main class, but it's turning into a 10,000+ line file. Since each of the main 4 or 5 components (classes) can be used independently without the need to access any of the others, I decided to try to write it as 4 or 5 separate, standalone applications that can be accessed at will from one parent class, hopefully keeping each file to 2,500 lines or less. I'm early enough in development that I can rewrite it according to your template. Thanks!Chemotaxis
T
0

have you tried this?

Runtime.getRuntime().exec("myClass1 [args]"); //put all args as you used in command

Also, handle/catch the exceptions, as needed.

Trishatriskelion answered 8/9, 2015 at 18:25 Comment(1)
:) That answers another question I had, as well - how to start an external program/application and capture the results. Thanks!Chemotaxis
C
0

I was right; it was a no-brainer. That's what I get for writing code on 4 hours of sleep:

myClass2 class2 = new myClass2(); 
try { 
 class2.start(stage); 
} catch (Exception e) { e.printStackTrace(); } }
Chemotaxis answered 8/9, 2015 at 18:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.