为什么 javac 在 @Override 注释上失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2335655/
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 is javac failing on @Override annotation
提问by skiphoppy
Eclipse is adding @Override annotations when I implement methods of an interface. Eclipse seems to have no problem with this. And our automated build process from Cruise Control seems to have no problem with this. But when I build from the command-line, with ant running javac, I get this error:
当我实现接口的方法时,Eclipse 正在添加 @Override 注释。Eclipse 似乎对此没有问题。我们来自 Cruise Control 的自动化构建过程似乎没有问题。但是当我从命令行构建时,ant 运行 javac,我得到这个错误:
[javac] C:\path\project\src\com\us\MyClass.java:70: method does not override a method from its superclass
[javac] @Override
[javac] ^
[javac] 1 error
Eclipse is running under Java 1.6. Cruise Control is running Java 1.5. My ant build fails regardless of which version of Java I use.
Eclipse 在 Java 1.6 下运行。Cruise Control 运行的是 Java 1.5。无论我使用哪个版本的 Java,我的 ant 构建都会失败。
采纳答案by Joshua McKinnon
The @Override
annotation spec changed in Java 1.6. In Java 1.5, the compiler did not allow the @Override
annotation on implemented interface methods, but in 1.6 it does. First search result I found is a blog post here.. It was not well documented, but it did change.
该@Override
注释规范用Java 1.6改变。在 Java 1.5 中,编译器不允许@Override
对已实现的接口方法进行注释,但在 1.6 中允许。我找到的第一个搜索结果是这里的一篇博客文章。. 它没有很好的记录,但它确实发生了变化。
Eclipse is adding it because your Eclipse is set for 1.6 compliance. You should try to keep your build and eclipse environments on the same version of Java. It's unclear to me by your specifying Cruise Control is running Java 5 on whether or not it is compiling using a separate JDK6 or not.
Eclipse 添加它是因为您的 Eclipse 设置为符合 1.6。您应该尝试将构建和 eclipse 环境保持在同一版本的 Java 上。我不清楚您是否指定 Cruise Control 正在运行 Java 5,以确定它是否使用单独的 JDK6 进行编译。
Separate from the above 1.5 vs 1.6 @Override
annotation rules, remember that Eclipse has its own compiler implementation (not javac
) and will occasionally have different behavior. Whenever something compiles in Eclipse, but not Ant or Maven, you will need to find a way to make both compilers happy.
与上面的 1.5 和 1.6@Override
注释规则分开,记住 Eclipse 有自己的编译器实现(不是javac
)并且偶尔会有不同的行为。每当某些东西在 Eclipse 中编译,而不是 Ant 或 Maven 时,您将需要找到一种方法让这两个编译器都满意。
回答by Martin
I can't really explain the problem you're seeing but it seems to be related to the fact that JDK 5will not allow @Override
on implemented methods of an interface, only on overridden methods present in a super class.
我无法真正解释您所看到的问题,但这似乎与JDK 5不允许接口的已@Override
实现方法,仅允许存在于超类中的重写方法的事实有关。
JDK 6will allow @Override
on any of them.
JDK 6将允许@Override
它们中的任何一个。
If your ant build fails it may be passing a source
parameter to javac, asking for JDK 5 compliance.
如果您的 ant 构建失败,它可能会source
向 javac传递一个参数,要求符合 JDK 5。
回答by ChrisH
回答by Jishnu
回答by matthieus
The direct answer to the question "Why" an error is raised by javac when @Override is used in the context of a method implementation is actually in the java specifications:
在方法实现的上下文中使用@Override 时,javac 会引发“为什么”这个问题的直接答案实际上是在 java 规范中:
"The rationale for this is that a concrete class that implements an interface will necessarily override all the interface's methods irrespective of the @Override annotation, and so it would be confusing to have the semantics of this annotation interact with the rules for implementing interfaces."
“这样做的基本原理是,实现接口的具体类必然会覆盖接口的所有方法,而不管 @Override 注释如何,因此让该注释的语义与实现接口的规则交互会令人困惑。”
See http://java.sun.com/docs/books/jls/third_edition/html/interfaces.html#9.6.1.4
见http://java.sun.com/docs/books/jls/third_edition/html/interfaces.html#9.6.1.4
But apparently someone changed his mind for java 1.6 and 1.5 u21...
但显然有人改变了对 java 1.6 和 1.5 u21 的想法……
回答by Glenn Yu
A lot of people, including me, got busted by this. See here for a bigger SO discussion
包括我在内的很多人都被这件事搞砸了。请参阅此处进行更大的 SO 讨论
回答by M.Kosmal
Ensure that there is only one definition of that interface.
确保该接口只有一个定义。
Example: HttpServletRequest
示例:HttpServletRequest
This is an interface with different specs depending on provider.
这是一个具有不同规格的接口,具体取决于提供商。
Compare pax-web-jetty and apache-felix-jetty. They have different methods.
比较 pax-web-jetty 和 apache-felix-jetty。他们有不同的方法。
回答by Torsten Barthel
I have had the same problem when building a project with ANT. The solution to the problem was to change the following property inside the build.properties file:
我在用 ANT 构建项目时遇到了同样的问题。该问题的解决方案是更改 build.properties 文件中的以下属性:
javac.compiler=org.eclipse.jdt.core.JDTCompilerAdapter
to:
到:
javac.compiler=modern
That solved the problem and the project got compiled and deployed successfully.
这解决了问题,项目成功编译和部署。