Start a Standalone Application in Java

0 votes

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?

Mar 5, 2019 in Java by Sushmita
• 6,920 points
3,287 views

1 answer to this question.

0 votes

You can make this work by calling start(...) directly on a new instance of one of the Applicationsubclasses, 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 Applicationshould 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.

answered Mar 6, 2019 by developer_1
• 3,350 points

Related Questions In Java

0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20, 2019 in Java by Sirajul
• 59,230 points
2,311 views
0 votes
2 answers

How to create a 2-D array in java?

int[][] multi = new int[5][]; multi[0] = new ...READ MORE

answered Jul 16, 2018 in Java by Daisy
• 8,140 points
1,545 views
0 votes
3 answers

How to read a Text File in Java?

You can use readAllLines and the join method to ...READ MORE

answered Jul 28, 2018 in Java by samarth295
• 2,220 points
2,489 views
0 votes
3 answers

How to reverse a string in java?

public static String reverse(String s) { ...READ MORE

answered Aug 17, 2018 in Java by samarth295
• 2,220 points
1,570 views
+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar 81,223 views
+1 vote
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,500 points
8,359 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,500 points
17,433 views
0 votes
1 answer

Selenium JARS(Java) missing from downloadable link

Nothing to worry about here. In the ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,500 points

edited Aug 4, 2023 by Khan Sarfaraz 4,876 views
0 votes
3 answers

Check if a String is numeric in Java

Java 8 Lambda Expression is used: String someString ...READ MORE

answered Sep 3, 2018 in Java by Daisy
• 8,140 points
3,699 views
0 votes
3 answers

How can I add new elements to an Array in Java

String[] source = new String[] { "a", ...READ MORE

answered Sep 19, 2018 in Java by Sushmita
• 6,920 points
13,192 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP