3

Im new to Java and I just wrote some Code in which I used two classes with main methods. Id like to execute both main methods, one after another. Is there a possibility to execute both of them at once in a specified order?

imFirst.java

public class imFirst { public static void main(String[] args) { System.out.println("I want to be the first one executed!"); } } 

imSecond.java

public class imSecond { public static void main(String[] args) { System.out.println("I want to be the second one executed!"); } } 

these are in one package, executed via eclipse.

5
  • 2
    Yes. But post your code first. Commented Oct 17, 2017 at 19:44
  • I don't think posting my whole code d be good, but i added a simple one. Commented Oct 17, 2017 at 20:08
  • Why do you use two main methods in the first place? A main-method is the first part in the whole Java chain. You should think of moving both your prints into regular methods and then call them from a dedicated main-method. Commented Oct 17, 2017 at 20:18
  • Well, i was just curious whether this is possible or not. Commented Oct 17, 2017 at 20:22
  • Well, Java will always just start one main method. It's your job as programmer to then specify inside the main method what the program should do. So there is no option such that Java automatically starts the other main after the first has finished. However you could create a simple batch/script-file for such a purpose. Commented Oct 17, 2017 at 20:33

2 Answers 2

5

You can call imSecond's main from imFirst:

public class imFirst { public static void main(String[] args) { System.out.println("I want to be the first one executed!"); imSecond.main(args); } } 

Or can be the opposite:

public class imSecond { public static void main(String[] args) { System.out.println("I want to be the second one executed!"); imFirst.main(args); } } 

Do it depending of your needs. But don't do both things at the same time or you can get an endless loop of both methods calling each other.

As a side note: use proper java naming conventions. Class names should be CamelCase.

Sign up to request clarification or add additional context in comments.

2 Comments

Better yet, don't mess with either. Create a new class RunAll with a main method that calls the others. Then you can choose to run the first by itself, the second by itself, or both.
@Andreas I agree. But it could be possible that OP needs to stick to that scheme. In that case it is helpful to know that other main-methods can be called like regular methods as well.
2

Quick-Fix

You can call a main-method like every other regular method as well:

public static void main(String[] args) { imFirst.main(null); imSecond.main(null); } 

Better approach

But you should first think of why you even need two main methods at all. A main method is the first thing in the whole Java chain and usually you only use one for every complete program. The purpose is to simply start the program, most times its just a call to a dedicated class like:

public static void main(String[] args) { ProgramXY programXY = new ProgramXY(); programXY.init(); programXY.start(); } 

So I recommend you to simply move both print statements into own classes and methods and then simply call them from one main method:

The utility class:

public class ConsolePrinter { public static void println(String line) { System.out.println(line); } } 

The only main-method:

public static void main(String[] args) { ConsolePrinter.println("I want to be the first one executed!"); ConsolePrinter.println("I want to be the second one executed!"); } 

More general

Or for a more general purpose:

First class:

public class FirstClass { public void firstMethod() { // ... } } 

Second class:

public class SecondClass { public void secondMethod() { // ... } } 

The only main method:

public static void main(String[] args) { FirstClass first = new FirstClass(); SecondClass second = new SecondClass(); first.firstMethod(); second.secondMethod(); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.