Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure Eclipse to compile using Oracle javac 1.7.0_09?

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?

like image 781
cane Avatar asked Jan 06 '13 19:01

cane


2 Answers

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

like image 127
aleroot Avatar answered Oct 09 '22 00:10

aleroot


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.

like image 28
bmargulies Avatar answered Oct 09 '22 00:10

bmargulies