6

I have the following code in Eclipse(Helios)/STS which runs and prints console output when doing a Run As> Java Application, in spite of obvious compilation issues

public interface ITest{
    String func();
}

public static class Test implements ITest{
    void printFunc(){
        System.out.println("Inside Test Function");
    }
}

public static void main(String[] args) {        
    Test test = new Test();
    test.printFunc();
}

Can anyone pinpoint the reasoning behind this Eclipse functioning.

Note: Doing a javac externally obviously fails to compile.

Anurag Sen
  • 75
  • 5

2 Answers2

1

It might have been that you have coded the class successfully before the errors. Eclipse auto-compiles your file while you are coding. Just then, you happen to have errors.. then you decide to run as Java Application, Eclipse will run the most recent compiled class.

I tried your code, implemented the necessary methods to remove the errors, then removed it again to put back the errors.. sure enough, it printed out "Inside Test Function". I also tried commenting out System.out.println("Inside Test Function"); and it still printed out.

In another try, I created another class, added your code, then run (without implementing the errors to avoid auto-compiling), then it printed out an error..

java.lang.NoSuchMethodError: main
Exception in thread "main" 
Russell Gutierrez
  • 1,372
  • 8
  • 19
  • How'd you get a `java.lang.NoSuchMethodError: main` error. Did you put the exact same code in the new class you created. I tried to recreate the scenario like you said, but my experience is different as stated. Everytime I save the jave file , I get a matching console response and not a 'cached' one (eg. the commenting out Sysout part). Just want to check if you are _building_ the file after saving it?' – Anurag Sen Oct 10 '12 at 11:39
1

Eclipse's Java compiler is designed to cope with flaky, non-compiling code. It will add whatever stuff is necessary to the code to get it to compile.

See this question What is the difference between javac and the Eclipse compiler?

Community
  • 1
  • 1
artbristol
  • 32,010
  • 5
  • 70
  • 103