views:

797

answers:

5

I have some Java code that was written using Eclipse and the Java 6 SDK, so methods that implement an interface are annotated with @Override - an annotation that is legal in Java 6, but not in Java 5.

I'd like to compile the same code using the Java 5 SDK (javac on Mac OS X 10.5). Everything compiles and runs fine except for the @Override annotations. Is there any way I can get javac to ignore the @Override annotations for this project, or is the only solution to remove them all?

+6  A: 

Unfortunately, the only way is to actually remove the annotations.

If you do want to have your code compile and run with Java 5, you should develop targeting Java 5. Otherwise, you might accidentally rely on a Java 6 specific SDK methods and such.

notnoop
One could easily remove them via a pre-processing step however.
Joachim Sauer
A: 

@Override is valid in java 5 -> http://java.sun.com/j2se/1.5.0/docs/api/index.html?java/lang/Override.html

I recommend that you check your project's compiler settings in Eclipse, perhaps at Project -> Compiler -> Error/Warnings -> Annotations?

Yoni
+1 - I was going to say the same thing. I use @Override too and I'm still on jdk 1.5.
PSpeed
@Override's semantics changed bwetween Java 5 and 6 - in 5, it caused a compiler error when used on a method implementing an interface. In 6, it causes a warning when *not* used on such a method. The question is about how to deal with this difference.
Michael Borgwardt
Yes @Override is valid in Java 5. But the specification has changed for Java6. In Java6, you can know annotate methods that implement methods of an interface. This was not possible in Java6.
Mihir Mathuria
"Indicates that a method declaration is intended to override a method declaration in a superclass" -- method-method override is supported, but method-interface override is not! (Or someone please correct me)
Tim
It is as I wrote: method-interface override is supported since Java 6, but unfortunately this change is not documented anywhere (I believe there's an official bug concerning the API doc not being updated).
Michael Borgwardt
One of a number of bugs on this issue: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5008260
Ash
A: 

Java 6 is available for OS X. Check software update. When you have it, set it to be the default java version, and then compile.

Thorbjørn Ravn Andersen
That's not really a solution when he wants to target Java 5 as well.
Joachim Sauer
You sidestep OP's question.
Adrian
+2  A: 

You can use JDK6 to compile 1.5 code. Use -bootclasspath, -target, and -source. Alternatively, I believe the Eclipse compiler treats @Override the same (this might be wrong!).

1.5 has finished its End of Service Life period, and I suggest letting it rot.

Tom Hawtin - tackline
+1 For suggesting -target and -source. Would be interesting to see if this works.
Alexander Pogrebnyak
Does this work, did you try it? ^^
Adrian
+1  A: 

Apparently you can upgrade to Java 1.5 u21 and this will fix the problem:

http://stackoverflow.com/questions/2335655/why-is-javac-failing-on-override-annotation

skiphoppy