Strange compiler difference between Eclipse and javac

java, javac

Solution

This looks like a bug, as reported on Oracle's bug database here.

According to the JLS §7.5, the order of `import`-statements should not matter.

Problem

The following snippet (abstracted from real-world code) compiles and runs in Eclipse. package1/Outer.java: ``` package package1; import package1.Outer.Mid.Inner; import package2.Bar; public class Outer { final Mid mid = new Mid(); public Outer() { mid.setInner(new Inner() { @Override public void foo() { System.out.println("In Outer.foo()"); } }); } public static class Mid implements Bar { private Inner inner; public void setInner(Inner inner) { this.inner = inner; } public Inner getInner() { return this.inner; } @Override public void bar() {} interface Inner { void foo(); } } } ``` package2/Bar.java: ``` package package2; public interface Bar { void bar(); } ``` However, it fails with this error when compiling using javac: ``` package1\Outer.java:31: cannot find symbol symbol : class Bar location: class package1.Outer public static class Mid implements Bar { ^ package1\Outer.java:42: method does not override or implement a method from a supertype @Override ^ 2 errors ``` Now, if I switch the order of the import statements, like so: ``` import package2.Bar; import package1.Outer.Mid.Inner; ``` ...then it compiles in both Eclipse and javac. Clearly the order of the import statements seems to matter...but why? Notes: - I tested this using Java JDK 1.6.0_30, and also Java JDK 1.7.0_21. If this is a bug that has since been fixed, that would be good to know. - It seems strange to me that the `package1.Outer.Mid.Inner` import is even necessary, given the `Inner` interface is nested within Outer.java, but both Eclipse and javac seem to require it - I discovered this problem trying to run an Ant build of production code that had a similar structure. Everything was building fine in Eclipse, but the Ant script just refused to go through.

Original source