9

I'm using JDK1.6. When I implement an interface and in the implementing class, if I give @override before my function names, Eclipse throws an compilation error. i.e. below code is wrong according to Eclipse.

public class SomeListener implements ServletContextListener {
    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
       // code
    }
    /* other overridden methods here */
}

If I remove @Override annotation, then the code compiles fine. Does it mean that JDK1.6 does not require us to prefix the @override annotation anymore?

Veera
  • 32,532
  • 36
  • 98
  • 137
  • 1
    What , specifically, is the error eclipse gives you ? – nos Sep 01 '10 at 14:23
  • 1
    You are absolutely certain you are actually overriding the method and not overloading it? – Thorbjørn Ravn Andersen Sep 01 '10 at 14:24
  • Eclipse's error message: The method contextDestroyed(ServletContextEvent) of type OgnlListener must override a superclass method – Veera Sep 01 '10 at 14:28
  • What is OgnlListener? That is not in your example. – Jacob Tomaw Sep 01 '10 at 14:38
  • oops.. actually OgnlListener was my original class name. I renamed it before posting it in stackoverdflow, but forgot to do the same in above comment. :) – Veera Sep 01 '10 at 16:04
  • I actually want the reverse. I have a project here that someone else wrote - which does not have any \@Override annotations. I would like the compiler / Eclipse to complain if the \@Override annotation is missing on overridden method implementations. – Kevin Parker Jul 06 '12 at 17:24

7 Answers7

27

You probably need to set the compiler compliance level in eclipse. This can be found in Window->Preferences->Java->Compiler

If the compiler preferences are still set to 1.5 the compiler will barf on the override annotation.

Edit: Also check compiler compliance level on a per project basis if you've set those to anything else than default.

Gennadiy
  • 17,657
  • 4
  • 26
  • 21
  • +1: I just got burned by this. Apparently one of my coworkers who uses Eclipse has his project set to 1.6, despite Maven setting the compiler version to 1.5 (and me using m2e to import it as a Maven project...). – Powerlord May 03 '12 at 15:31
  • In my case it was because in the pom.xml, for maven build, the plugin maven-compiler-plugin was configured to work for 1.5 version java source and target. Changing the values in it to the following fixed it: org.apache.maven.plugins maven-compiler-plugin 2.3.2 1.6 1.6 – T A Jun 27 '13 at 07:42
2

The Java Compiler settings can be at multiple places based on the configuration You choose, One way is to Window->Preferences->Java->Compiler, change that to 1.6 minimum, if it was set to some earlier version. Another way is Right Click on Project-> Properties ->Java Compiler ->JDK Compliance ->Select JDK1.6 minimum, click apply.

After you make the changes, let the project build, it builds and take the changes into affect.

If none of the above options work - Try adding the rt.jar to classpath, it will fix the problem.

kanaparthikiran
  • 523
  • 12
  • 15
1

The @Override annotation changed in Java 1.6 version. In Java 1.5, the compiler didn't allow @Override annotation on implemented interface methods, from 1.6 it does.

Java Compiler

You must change java compiler version in properties project -> Java Compiler

borchvm
  • 3,533
  • 16
  • 44
  • 45
0

JDK1.6 definitely supports it. I'm not sure why you would have issues.

What error are you seeing? The only thing I can think of is to make sure that you are using the correct JDK in your project settings. Maybe you are compiling against an older JDK?

Riaan Cornelius
  • 1,933
  • 1
  • 14
  • 17
0

No the @Override annotation is still used. You should check that the contextDestroyed method is really present in the ServletContextListener interface, and check the imported package for this interface.

Manuel Selva
  • 18,554
  • 22
  • 89
  • 134
0

It sounds like your compiler is set for Java 5, when @Override on interfaces wasn't allowed.

Noel M
  • 15,812
  • 8
  • 39
  • 47