ChatGPT解决这个技术问题 Extra ChatGPT

Why does Eclipse complain about @Override on interface methods?

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.

But it seems the source is targeted to Java 6?
Indeed. Java 5 was a late requirement for mac support.

e
erickson

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:

Open the Window > Preferences dialog Browse to Java > Compiler. 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.


No, it won't. It will produce an error message "javac: source release 1.6 requires target release 1.6".
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.
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.
@erickson does the -target -source downgrade works for 1.7 to 1.6 too? stackoverflow.com/q/8869869/632951
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
R
Randika Vishman

Do as follows:

Project -> Properties -> java compiler ->

Enable project specific settings - 'yes'

Compiler compliance - 1.6

generated class files and source compatibility - 1.5


Thanks - didn't realise each project also has it's own settings
in my case I needed to disable this option.
g
gdoumenc

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


Score! You were right, my project facet was overriding global compliance settings. Thanks!
c
chriz

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


a
akarnokd

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.


Can you provide any specifics?
It is on the same page as stated by erickson.
The project is several GB large. A simple "copy" isn't even practical. :/
In this case, you'll need to ask for change permission. Or create a branch for the 1.5 compatible sources and manually fix the java files.
佚名

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


P
Peter Tseng

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

Steps:

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


G
Gen

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


I
IstaLibera

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.


S
SOFe

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.