7

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?

cane
  • 892
  • 1
  • 10
  • 16
  • 1
    Hmm, that is a bug in ecj, you should report that – fge Jan 06 '13 at 19:57
  • Your actual question is a duplicate of [Have two JDK installed, how to switch the java compiler in Eclipse](http://stackoverflow.com/questions/5403838/have-two-jdk-installed-how-to-switch-the-java-compiler-in-eclipse) – Brian Roach Jan 06 '13 at 20:02

2 Answers2

9

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) ...

Community
  • 1
  • 1
aleroot
  • 71,077
  • 30
  • 176
  • 213
  • 4
    That was not the question, actually. I think the OP is aware of that. The question is how to tell Eclipse to use javac proper instead of its own frontend. – fge Jan 06 '13 at 19:57
  • 1
    @Logan as explained in the linked "older post" ECJ can create (partial) .class files even from code with compile errors. In this particular case you were "lucky" that the .class file doesn't even need to throw an exception at the error location, because an error-free method actually exists (by just dropping the other method). Understanding this should suffice to be happy with ECJ - no need to jump through hoops to let Eclipse invoke javac via ant an loose much of the immediate feedback... – Stephan Herrmann Aug 01 '15 at 19:22
5

Eclipse will never use javac. Its ability to do dynamic highlighting is intimately connected with its own compiler, which has special abilities to operate incrementally.

If you want an IDE that uses javac, you might investigate intellij.

bmargulies
  • 97,814
  • 39
  • 186
  • 310