I am trying to compile following piece of code:
public class DuplicateMainExample {
public static void main(String[] args) {
System.out.print("A1");
}
public static void main(String... args) {
System.out.print("A2");
}
}
In Eclipse it’s working fine, but with warnings on the both methods – “Duplicate method main(String[]) in type DuplicateMainExample“
Using javac (java version “1.7.0_09”) I have an compilation error:
>javac DuplicateMainExample.java
DuplicateMainExample.java:8: error: cannot declare both main(String...) and main
(String[]) in DuplicateMainExample
public static void main(String... args) {
^
1 error
How to compile in Eclipse using javac?
Simply because you have declared the same method with exactly the same signature twice … Only one main method for class should be declared .
Eclipse have embedded its own compiler and in the case of two main methods it gets the last one, the eclipse compiler and the javac compiler are two different compilers …
Take a look at this older post for more information …
If you want to compile with javac you could try using the ant javac adapter from within eclipse … However i think that ECJ is even better than javac(my opinion) …