Why is javac failing on @Override annotation
Asked Answered
F

8

55

Eclipse is adding @Override annotations when I implement methods of an interface. Eclipse seems to have no problem with this. And our automated build process from Cruise Control seems to have no problem with this. But when I build from the command-line, with ant running javac, I get this error:

[javac] C:\path\project\src\com\us\MyClass.java:70: method does not override a method from its superclass
[javac]     @Override
[javac]      ^
[javac] 1 error

Eclipse is running under Java 1.6. Cruise Control is running Java 1.5. My ant build fails regardless of which version of Java I use.

Fustanella answered 25/2, 2010 at 16:30 Comment(0)
J
101

The @Override annotation spec changed in Java 1.6. In Java 1.5, the compiler did not allow the @Override annotation on implemented interface methods, but in 1.6 it does. First search result I found is a blog post here.. It was not well documented, but it did change.

Eclipse is adding it because your Eclipse is set for 1.6 compliance. You should try to keep your build and eclipse environments on the same version of Java. It's unclear to me by your specifying Cruise Control is running Java 5 on whether or not it is compiling using a separate JDK6 or not.

Separate from the above 1.5 vs 1.6 @Override annotation rules, remember that Eclipse has its own compiler implementation (not javac) and will occasionally have different behavior. Whenever something compiles in Eclipse, but not Ant or Maven, you will need to find a way to make both compilers happy.

Here's a screenshot of changing the compiler in eclipse

Janerich answered 25/2, 2010 at 16:41 Comment(6)
This is a great answer; I have one thing to add: if for whatever reason you want to stay on JDK 1.5, you just have to upgrade to the latest update release. I'm using u21 and it compiles these type of @Overrides just fine.Spermogonium
Cool - I'll add that to my answer.Janerich
I installed both JDK 1.5u21 and 1.5u22 and javac does not allow @Override on interface method implementations in either release. Below, MyRunnable implements java.lang.Runnable : c:/Java/jdk/1.5.0_21/bin/javac MyRunnable.java MyRunnable.java:3: method does not override a method from its superclass @Override public void run() { } ^ 1 error It is not mentioned in the release notes, either.Adamant
@djb, Verified - correcting answer. The behavior observed by Kevin must have been Eclipse, not javac.Janerich
I also had the same issue and I changed the JRE version to 1.6 through Maven, Run Configuration. Thanx a lot. +1ed.Olli
@KevinBourrillion do you have any release notes that confirm your statement? As djb writes, it cannot be confirmed.Schear
S
16

I can't really explain the problem you're seeing but it seems to be related to the fact that JDK 5 will not allow @Override on implemented methods of an interface, only on overridden methods present in a super class.

JDK 6 will allow @Override on any of them.

If your ant build fails it may be passing a source parameter to javac, asking for JDK 5 compliance.

Syringomyelia answered 25/2, 2010 at 16:38 Comment(0)
S
3

@Override tags for implemented methods are new to Java 1.6. In Java 1.5 @Override is only correct when overriding a method in a base class. Read more here and here.

Suricate answered 25/2, 2010 at 16:39 Comment(0)
O
3

The direct answer to the question "Why" an error is raised by javac when @Override is used in the context of a method implementation is actually in the java specifications:

"The rationale for this is that a concrete class that implements an interface will necessarily override all the interface's methods irrespective of the @Override annotation, and so it would be confusing to have the semantics of this annotation interact with the rules for implementing interfaces."

See http://java.sun.com/docs/books/jls/third_edition/html/interfaces.html#9.6.1.4

But apparently someone changed his mind for java 1.6 and 1.5 u21...

Outgrow answered 26/3, 2010 at 14:47 Comment(0)
P
1

A lot of people, including me, got busted by this. See here for a bigger SO discussion

Palladino answered 17/9, 2012 at 13:11 Comment(0)
E
0

Eclipse would be pointing to 1.6 version of Java rather than 1.5. See here for configuring java version in eclipse.

Eskridge answered 25/2, 2010 at 18:4 Comment(0)
J
0

Ensure that there is only one definition of that interface.

Example: HttpServletRequest

This is an interface with different specs depending on provider.

Compare pax-web-jetty and apache-felix-jetty. They have different methods.

Johansen answered 12/2, 2014 at 15:37 Comment(0)
A
0

I have had the same problem when building a project with ANT. The solution to the problem was to change the following property inside the build.properties file:

javac.compiler=org.eclipse.jdt.core.JDTCompilerAdapter

to:

javac.compiler=modern

That solved the problem and the project got compiled and deployed successfully.

Adigun answered 29/8, 2017 at 21:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.