Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ANT doesn't understand Java 5 @Override Annotation

 
Lisa Ray
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I've come across a strange issue. I'm using Eclipse IDE with Java SE 6. When I correctly override a method from a parent class/interface and use @Override annotation, Eclipse doesn't complaint (as expected because it's a valid override). However, when I compile with ANT (version 1.7) I get the following compilation error:




This is strange. Any help shall be highly appreciated.

Lisa
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ant isn't doing the compiling, whatever compiler you have configured is.
 
Lisa Ray
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear wrote:
Ant isn't doing the compiling, whatever compiler you have configured is.


I've configured compiling as shown below:
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the original compile error, it looks like it does understand @Override Annotation but disagrees that it is valid. Perhaps show us more code (like at least the two method signatures)
 
Lisa Ray
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If there was some issue with the method override, the Eclipse compiler would have reported that. Still, I'm sharing the code.

POJI



POJO




Ant


 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like it is a difference between java 5 & 6.

[EDIT] OK, that looks like one of my typical terse replies. I should add a little more info...

I use eclipse. I set the JRE to 1.6 and set the compiler compliance level to 6.0. Eclipse puts in the @Override in the class that implements the interface if I have eclipse put the method in (create the class, but no method and it complains that the class needs to implement something and I have it "add unimplemented methods"). If I change the compliance level to 5.0 it then complains about @Override. When I have eclipse add the method (5.0 still) it doesn't add the @Override.

(Maybe it was better without the added explanation?)
 
Lisa Ray
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carol wrote:
Looks like it is a difference between java 5 & 6.


I tried doing the following changes and failed:



And,


Carol, What needs to be done to get off this issue?


In anticipation.
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't tried it out as I never use target/source with javac. Looks like 1.6 should work,possibly 6. Maybe I'll go try that.

I'd try running ant verbosely (ant -v) to see what version it is trying to use. Perhaps it is 5 and can't do 6.


[EDIT] Oh by the way, welcome to JavaRanch!!!
 
Lisa Ray
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
This is what I did to get out of the mess and make Ant Happy.

Ant and Eclipse are happy when:
Java compiler's compliance is set to Java 6
Ant Global Entries point to Java 6 tools.jar

Ant and Eclipse are unhappy when:
Java compiler's compliance is set to Java 6
Ant Global Entries point to Java 5 tools.jar


Eclipse is unhappy when

Java compiler's compliance is set to Java 6 (this is strange even when it's a valid override). Can somebody explain this weird behavior.




 
Tim Holloway
Saloon Keeper
Pie
Posts: 18276
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, as you've seen, it's best when you keep to the same Java environment all the way through.

Your message "method does not override a method from its superclass " almost certainly comes from classloading issues. Part of Java's security model is that just because 2 classes have the exact same name and signatures doesn't mean that Java will consider them to be the same class. It's also important that they be on the same classpath branch.

As for Eclipse's problems, the first thing you need to do is ensure that you've installed the desired JDK into Eclipse and that the project in question is going to refer to it, either because it's the default workspace JRE or by an explict override. The Java Version Compliance setting in the Build Properties dialog is primarily for Eclipse to use as guidance on how to control editing (for example, flagging "enum", and use of annotations/generics/iterators), but the underlying selected JDK determines how classes get resolved.
 
Geoffrey Falk
Ranch Hand
Posts: 171
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The difference between Java 1.5 and 1.6 is:

in 1.5, @Override is accepted only in the case of extending a class. If you are implementing a method in an interface, then you cannot use this annotation.

In 1.6, @Override is acceptable when implementing a method from an interface.

If you need Ant to accept the 1.6 usage, then set the compiler source compliance to "1.6" in your build.xml: <javac source="1.6"..... />

Geoffrey
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interestingly, this change from Java 5 to Java 6 apparently wasn't documented anywhere: http://blogs.sun.com/ahe/entry/override_snafu thus taking many people (including myself :-) by surprise.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34837
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Interestingly, this change from Java 5 to Java 6 apparently wasn't documented anywhere: http://blogs.sun.com/ahe/entry/override_snafu thus taking many people (including myself :-) by surprise.

And a Java 6 compiler in Java 5 mode doesn't tell you about it. Which means Ulf has caught me using @Override in a way that doesn't work on a real Java 5 compiler a number of times
 
Aneel Sahoo
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anil Sahu wrote:Thanks everyone for posting the solution. It all worked fine when I pointed Java to 6 instead of 5.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic