Java 为什么 Eclipse 会在接口方法上抱怨 @Override?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/987973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does Eclipse complain about @Override on interface methods?
提问by Stefan Kendall
I have an existing project that uses @Override
on methods that override interfacemethods, 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.
我有一个现有项目,它使用@Override
覆盖接口方法的方法,而不是超类方法。我无法在代码中更改这一点,但我希望 Eclpse 停止抱怨注释,因为我仍然可以使用 Maven 进行构建。
How would I go about disabling this error?
我将如何禁用此错误?
Note: Due to project requirements, I need to compile for Java 1.5.
注意:由于项目要求,我需要为Java 1.5编译。
采纳答案by 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.
@Override
在实现接口声明的方法上使用注解仅从 Java 6 开始有效。这是 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:
确保您的 IDE 项目设置为使用 Java 6 JRE,并且“源代码兼容性”设置为 1.6 或更高版本:
- Open the Window > Preferences dialog
- Browse to Java > Compiler.
- There, set the "Compiler compliance level" to 1.6.
- 打开窗口 > 首选项对话框
- 浏览到 Java > 编译器。
- 在那里,将“编译器合规级别”设置为 1.6。
Remember that Eclipse can override these global settings for a specific project, so check those too.
请记住,Eclipse 可以覆盖特定项目的这些全局设置,因此也请检查这些设置。
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.
Java 5 下的错误不仅仅在 Eclipse 中;javac
直接从命令行使用会给你同样的错误。它不是有效的 Java 5 源代码。
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.
但是,您可以-target 1.5
为 JDK 6's指定选项javac
,这将从 Java 6 源代码生成 Java 5 版本类文件。
回答by 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).
您可以更改编译器设置以接受 Java 6 语法但生成 Java 5 输出(我记得)。如果您的运行时需要,将“生成的类文件兼容性”设置得更低一些。 更新:我检查了 Eclipse,但如果我将源兼容性设置为 1.6,并将类兼容性设置为 1.5,它会抱怨。如果不允许使用 1.6,我通常会手动注释掉源代码中有问题的 @Override 注释(这对您的情况没有帮助)。
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.
更新 2:如果您只进行手动构建,您可以编写一个小程序,将原始项目复制到一个新项目中,从 Java 源代码中去除 @Override 注释,然后您只需在 Eclipse 中点击 Clean 项目。
回答by akarnokd
You can also try Retroweaver to create the Java5 version from Java6 classes.
您还可以尝试使用 Retroweaver 从 Java6 类创建 Java5 版本。
回答by mitko
Do as follows:
执行以下操作:
Project -> Properties -> java compiler ->
项目 -> 属性 -> java 编译器 ->
- Enable project specific settings - 'yes'
- Compiler compliance - 1.6
- generated class files and source compatibility - 1.5
- 启用项目特定设置 - “是”
- 编译器合规性 - 1.6
- 生成的类文件和源兼容性 - 1.5
回答by gdoumenc
Check also if the project has facet. The java version may be overriden there.
还要检查项目是否有方面。java 版本可能会在那里被覆盖。
回答by Peter Tseng
Use Eclipse to search and replace (remove) all instances of "@Override". Then add back the non-interface overrides using "Clean Up".
使用 Eclipse 搜索和替换(删除)“@Override”的所有实例。然后使用“清理”重新添加非接口覆盖。
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.
- 选择包含源文件的项目或文件夹。
- 转到“搜索>搜索...”(Ctrl-H)以打开搜索对话框。
- 转到“文件搜索”选项卡。
- 在“包含文本”中输入“@Override”,在“文件名模式”中输入“*.java”。单击“替换...”,然后单击“确定”,以删除“@Override”的所有实例。
- 转到“窗口 > 首选项 > Java > 代码样式 > 清理”并创建一个新的配置文件。
- 编辑配置文件,并取消选中除“缺少代码 > 添加缺少的注释 > @Override”之外的所有内容。确保未选中“接口方法的实现”。
- 选择包含源文件的项目或文件夹。
- 选择“源 > 清理...”(Alt+Shift+s,然后是 u),然后选择“完成”以重新添加非接口覆盖。
回答by Henry
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 project
and eclipse. It worked for me
可以启用项目特定设置。选择项目Project > Properties > Java Compiler
,uncheck
在Enable project specific settings
或更改Jdk 1.6
及以上不忘对应JRE
。
如果它不起作用,请从 eclipse 中删除您的项目,删除.settings folders
, .project
,.classpath
文件。clean and build the project, import it back into eclipse and then reset your Java compiler. Clean and build your project
和日食。它对我有用
回答by Gen
I understood your problem, change your jdk from your jdk to greaterthan 1.5
我了解你的问题,将你的 jdk 从你的 jdk 更改为大于 1.5
回答by 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 restartof the Eclipe IDE helped.
通过配置将 IDE 项目设置为使用 Java 6 JRE 或更高版本,有时不会消除 Eclipse 错误。对我来说,重启Eclipe IDE 有帮助。
回答by Ranjan
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.
即使在 Windows 选项卡中将编译器合规性设置更改为 1.6 或 1.7,然后是首选项,然后是 Java,然后是编译器并设置编译器合规性,我仍然遇到这个问题。这个想法是我们需要转到项目文件夹,右键单击,Java 并将编译器合规性设置为 1.6 或更高版本。这对我有用。