Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
487 views
in Technique[技术] by (71.8m points)

java - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...