138

I have an existing project that uses @Override on methods that override interface methods, rather than superclass methods. I cannot alter this in code, but I would like Eclpse to stop complaining about the annotation, as I can still build with Maven.

How would I go about disabling this error?

Note: Due to project requirements, I need to compile for Java 1.5.

Ajinkya
  • 22,324
  • 33
  • 110
  • 161
Stefan Kendall
  • 66,414
  • 68
  • 253
  • 406

10 Answers10

272

Using the @Override annotation on methods that implement those declared by an interface is only valid from Java 6 onward. It's an error in Java 5.

Make sure that your IDE projects are setup to use a Java 6 JRE, and that the "source compatibility" is set to 1.6 or greater:

  1. Open the Window > Preferences dialog
  2. Browse to Java > Compiler.
  3. There, set the "Compiler compliance level" to 1.6.

Remember that Eclipse can override these global settings for a specific project, so check those too.


Update:

The error under Java 5 isn't just with Eclipse; using javac directly from the command line will give you the same error. It is not valid Java 5 source code.

However, you can specify the -target 1.5 option to JDK 6's javac, which will produce a Java 5 version class file from the Java 6 source code.

erickson
  • 265,237
  • 58
  • 395
  • 493
  • No, it won't. It will produce an error message "javac: source release 1.6 requires target release 1.6". – Michael Borgwardt Jun 12 '09 at 18:07
  • 9
    No, it won't. It will produce a compiled class file. It only gives that error if you specify the -source 1.6 option, which isn't necessary to make this work. – erickson Jun 12 '09 at 18:20
  • You know, since I build manually anyway, I can just leave the source level at 1.6. I could not get any sort of mixed configuration working, and this seems simplest. – Stefan Kendall Jun 12 '09 at 18:32
  • @erickson does the -target -source downgrade works for 1.7 to 1.6 too? http://stackoverflow.com/q/8869869/632951 – Pacerier Jan 18 '12 at 15:32
  • This doesn't seem to work with the 1.8 JDK. -source 1.6 -target 1.5 says: javac: source release 1.6 requires target release 1.6 – Don Hatch Jul 03 '16 at 00:59
  • @DonHatch Did you try without a -source option? – erickson Jul 03 '16 at 03:04
  • @erickson Yes. -target 1.5 alone produces "javac: target release 1.5 conflicts with default source release 1.8". – Don Hatch Jul 03 '16 at 03:11
  • However, strangely, -source 1.5 -target 1.5 seems to allow @Override of interface methods, so maybe there's no need for 1.6 at all. Apparently the interface-@Override restriction was lifted in some later patch release of 1.5; see http://stackoverflow.com/questions/2135975/can-i-get-java-5-to-ignore-override-errors?lq=1#answer-2336850 – Don Hatch Jul 03 '16 at 03:22
  • You just saved me a lot of time. It appears that in my Eclipse, the problem is here but as mentioned by the other, not by the Maven compiler. – MadJlzz Sep 30 '16 at 15:31
  • @DonHatch that apparently lifted restriction is not confirmed, see http://stackoverflow.com/questions/2335655/why-is-javac-failing-on-override-annotation#comment3379487_2335752 – Amedee Van Gasse Oct 18 '16 at 11:50
41

Do as follows:

Project -> Properties -> java compiler ->

  • Enable project specific settings - 'yes'
  • Compiler compliance - 1.6
  • generated class files and source compatibility - 1.5
Randika Vishman
  • 7,983
  • 3
  • 57
  • 80
mitko
  • 411
  • 4
  • 2
12

Check also if the project has facet. The java version may be overriden there.

gdoumenc
  • 589
  • 7
  • 10
9

Project specific settings may be enabled. Select your project Project > Properties > Java Compiler, uncheck the Enable project specific settings or change Jdk 1.6 and above not forgetting the corresponding JRE.
Incase it does not work, remove your project from eclipse, delete .settings folders, .project, .classpath files. clean and build the project, import it back into eclipse and then reset your Java compiler. Clean and build your projectand eclipse. It worked for me

chriz
  • 1,339
  • 2
  • 16
  • 32
Henry
  • 91
  • 1
  • 5
8

You could change the compiler settings to accept Java 6 syntax but generate Java 5 output (as I remember). And set the "Generated class files compatibility" a bit lower if needed by your runtime. Update: I checked Eclipse, but it complains if I set source compatibility to 1.6 and class compatibility to 1.5. If 1.6 is not allowed I usually manually comment out the offending @Override annotations in the source (which doesn't help your case).

Update2: If you do only manual build, you could write a small program which copies the original project into a new one, strips @Override annotations from the java sources and you just hit Clean project in Eclipse.

akarnokd
  • 69,132
  • 14
  • 157
  • 192
1

Use Eclipse to search and replace (remove) all instances of "@Override". Then add back the non-interface overrides using "Clean Up".

Steps:

  1. Select the projects or folders containing your source files.
  2. Go to "Search > Search..." (Ctrl-H) to bring up the Search dialog.
  3. Go to the "File Search" tab.
  4. Enter "@Override" in "Containing text" and "*.java" in "File name patterns". Click "Replace...", then "OK", to remove all instances of "@Override".
  5. Go to "Window > Preferences > Java > Code Style > Clean Up" and create a new profile.
  6. Edit the profile, and uncheck everything except "Missing Code > Add missing Annotations > @Override". Make sure "Implementations of interface methods" is unchecked.
  7. Select the projects or folders containing your source files.
  8. Select "Source > Clean Up..." (Alt+Shift+s, then u), then "Finish" to add back the non-interface overrides.
Peter Tseng
  • 13,613
  • 4
  • 67
  • 57
1

You can also try Retroweaver to create the Java5 version from Java6 classes.

0

I understood your problem, change your jdk from your jdk to greaterthan 1.5

Gen
  • 2,400
  • 4
  • 24
  • 46
0

By configuring that the IDE projects are setup to use a Java 6 JRE or above sometimes does not remove the eclipse error. For me a restart of the Eclipe IDE helped.

IstaLibera
  • 580
  • 5
  • 22
0

Even after changing the compiler compliance setting to 1.6 or 1.7 from windows tab, then prefernces, then java, then compiler and setting the compiler compliance, I was still having this issue. The idea is that we need to go the the project folder, right click, Java and set compiler compliance to 1.6 or higer. This worked for me.

SOFe
  • 7,867
  • 4
  • 33
  • 61
Ranjan
  • 11
  • 2