3

I have several override methods like this:

@Override
public int compareTo(Property p) {
    return getText().compareTo(p.getText());
}

As a Java project, it works fine, but as a Maven project, it returns the following error:

The method compareTo(Property) of type Property must override a superclass method

After researching into this, I think I'm suppose to include my JRE System Library (jdk1.6_u25) as a dependency in my POM file, or is this a completely different problem all together?

Many thanks.

Kurtiss
  • 495
  • 2
  • 7
  • 22
  • You shouldn't need to add the JDK as a dependency. Can you confirm that your class is implementing `Comparable`? – Will Jun 10 '14 at 08:52
  • Setting the JDK version like that won't work; if you want to set the source and target version, do so as explained under http://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html However this is probably *not* the problem here. – blalasaadri Jun 10 '14 at 08:54
  • @Will yes the class is implementing `Comparable`. – Kurtiss Jun 10 '14 at 09:04

3 Answers3

8

You don't need another dependency. But by default, maven uses Java 5 language level, where @Override wasn't allowed for implementing interface methods. That was introduced in 6.

So you must configure the compiler plugin to use language level 6 like this:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                 <source>1.6</source>
                 <target>1.6</target>
            </configuration>
        </plugin>
    </plugins>
</build>
Ray
  • 3,084
  • 2
  • 19
  • 27
  • Thank you for your prompt reply, I've copied and pasted your code and it returns `The managed version could not be determined. The managed definition location could not be determined, probably defined by "import" scoped dependencies.` Many thanks. – Kurtiss Jun 10 '14 at 09:24
  • I've never sen that. Try adding `3.1` inside the `` section. – Ray Jun 10 '14 at 09:58
  • It's OK I've found out what the problem was and posted it as an answer. Thank you for your help. – Kurtiss Jun 10 '14 at 10:00
3

compareTo is a generic method. Generics are not used so compareTo(Object) is the only method you can override.

Please check that:

  1. Maven uses a java to compile that supports generics.
  2. Source-Level is >= 5.

execute mvn -V to see what version of java maven uses to compile.

Regards

Grim
  • 1,938
  • 10
  • 56
  • 123
  • Apache Maven 3.2.1, Java version 1.6.0_25 – Kurtiss Jun 10 '14 at 09:02
  • @Kurtiss did you do 'mvn -V' via commandline like you `mvn install` via commandline or do you use a maven-eclipse-integration for any of them? Notice it might make a difference. – Grim Jun 10 '14 at 10:53
  • Got this info through the command prompt, anyways it's OK I've found out what the problem was and posted it as an answer. Thank you for your help. – Kurtiss Jun 10 '14 at 11:15
3

Thank you all for your comments, a lot of you stated that Maven used Java 5 by default and could be the cause of the issue, and as a result, I was able to determine the problem through this answer:

Why is javac failing on @Override annotation

The JDK compiler's compliance level was set to 1.5 by default; once I set it to 1.6, the errors were removed.

Many thanks.

Community
  • 1
  • 1
Kurtiss
  • 495
  • 2
  • 7
  • 22