0

File Name = multiple_main_methods.java

class multiple_main_methods_two {
    public static void main(String[] args){
        System.out.println("Class second");
    }
}
class multiple_main_methods_one {
    public static void main(String[] args){
        System.out.println("Class first");
    }
}

Output

Class first

IDE used - IntelliJ IDEA

preetika
  • 11
  • 1
  • Why would / should it "give an error"? Perfectly valid to have multiple entry points. – luk2302 Sep 23 '22 at 07:36
  • and why do you think this should cause an error? – Stultuske Sep 23 '22 at 07:36
  • @XtremeBaumer from time to time IDE's allow it like this. – Stultuske Sep 23 '22 at 07:36
  • I'll post this as a comment since it's not a complete answer but the [spec](https://docs.oracle.com/javase/specs/jls/se17/html/jls-12.html#jls-12.1) says "The Java Virtual Machine starts execution by invoking the method main of some **specified** class or interface" which means the IDE compiled your source file, obtained two `.class` file and decided which one to run. Why it chose the second one (confusingly called "one") I don't know. – Federico klez Culloca Sep 23 '22 at 07:39
  • @FedericoklezCulloca I think the "first" is related to the class name – Stultuske Sep 23 '22 at 07:41
  • @Stultuske right, I meant "one" – Federico klez Culloca Sep 23 '22 at 07:42
  • Alright, yes I was wrong. I now have it working in eclipse with java 8. However, whenever I want to run the main method, I have to select the specific method I want to run. The run configuration define the main class as `stackoverflow.multiple_main_methods_one` and `stackoverflow.multiple_main_methods_two` – XtremeBaumer Sep 23 '22 at 07:42
  • @Stultuske if not error then why did it not print Class Second as the output, why it only printed Class first ? – preetika Sep 23 '22 at 07:52
  • @luk2302 can you suggest any source where i can read about multiple entry points ? – preetika Sep 23 '22 at 07:55
  • @preetika every class can have a main method. You can have thousands of main methods in each project, though it wouldn't be very helpful. Did you configure the class to use as entrypoint somewhere in your project/ide? – Stultuske Sep 23 '22 at 08:02
  • You can have multiple entry points, however only one can be called at a time. – XtremeBaumer Sep 23 '22 at 08:03
  • @Stultuske i did not configured i checked and found IntelliJ choose multiple_main_methods_one class. But then again why IntelliJ did not choose _two class ? – preetika Sep 23 '22 at 08:25
  • Does this answer your question? ["Error: Main method not found in class MyClass, please define the main method as..."](https://stackoverflow.com/questions/5407250/error-main-method-not-found-in-class-myclass-please-define-the-main-method-as) – pringi Sep 23 '22 at 13:59

2 Answers2

0

IntelliJ is choosing a class to execute. Remember, you execute the main method in a class, not in a file.

Check it in your Run configurations enter image description here

0

It is not giving error because your main methods belong to different classes i.e. multiple_main_methods_two and multiple_main_methods_one which is completely fine.