問題描述
當我實現接口的方法時,Eclipse 正在添加 @Override 注釋.Eclipse 似乎對此沒有任何問題.而我們來自 Cruise Control 的自動化構建過程似乎對此沒有任何問題.但是當我從命令行構建時,ant 運行 javac,我得到了這個錯誤:
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:
[javac] C:pathprojectsrccomusMyClass.java:70: method does not override a method from its superclass
[javac] @Override
[javac] ^
[javac] 1 error
Eclipse 在 Java 1.6 下運行.Cruise Control 正在運行 Java 1.5.無論我使用哪個版本的 Java,我的 ant 構建都會失敗.
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.
推薦答案
@Override
注釋規范在 Java 1.6 中發生了變化.在 Java 1.5 中,編譯器不允許在實現的接口方法上使用 @Override
注釋,但在 1.6 中卻允許.我找到的第一個搜索結果是 博客文章..沒有很好的文檔記錄,但它確實發生了變化.
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.
Eclipse 正在添加它,因為您的 Eclipse 設置為符合 1.6.您應該嘗試將您的構建和 Eclipse 環境保持在相同版本的 Java 上.我不清楚您指定 Cruise Control 是否正在運行 Java 5 是否使用單獨的 JDK6 進行編譯.
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.
與上面的1.5 vs 1.6 @Override
注解規則分開,記住Eclipse有自己的編譯器實現(不是javac
),偶爾會有不同的行為.每當某些東西在 Eclipse 中編譯,而不是在 Ant 或 Maven 中編譯時,您都需要找到一種方法讓兩個編譯器都滿意.
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.
這篇關于為什么javac在@Override注釋上失敗的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!