17

Situation

I have an OSGi project that I'm trying to migrate to Java 8. In my project, I have dependencies to third party libraries that I "OSGi-fied" (by just adding the MANIFEST.MF file and putting metadata into it). These libraries are checked out from read-only SVN repositories, so I just can checkout updates from then when needed and therefore I don't want to make any other changes than in the MANIFEST.MF file, since I cannot commit them.

Problem

However, these libraries use lots of anonymous Comparators like:

private static final Comparator heightComparator = new Comparator() {
    public int compare (Object o1, Object o2) {
        return ((Glyph)o1).getHeight() - ((Glyph)o2).getHeight();
    }
};

Now, apparently the java.util.Comparator interface has a whole bunch of new methods that need to be implemented (which, of course, leads to compilation errors). But i really want to avoid implementing them or switch to Lambda expressions because modifying the original source would most likely result in conflicts each time I check out newer revisions.

Java used to work hard on backwards compatibility and I wonder why such a simple and widely used part of the API needs so (relatively) much effort to migrate. Am I missing something or is it really unavoidable?

thobens
  • 1,729
  • 1
  • 15
  • 34
  • What compiler error are you getting? And just FYI, the extra methods added to the interface are all defender method. You don't need to implement them. And finally what do you want here? Do you just want to convert that code to lambda expression? – Rohit Jain Mar 22 '14 at 06:10

3 Answers3

22

I had a similar problem like thobens: Updated my Kepler 4.2.3 to use Java 8, set JAVA 8 as new JRE, etc. Like thobens I got an error on Comparators by eclipse requesting me to implement all unimplemented methods.

Finally this was caused by the old Compiler compilance level (1.7) - switching to 1.8 solved this trouble.

jandy
  • 221
  • 1
  • 2
  • 3
    Thanks. Here is how to: Right click the project in Package Explorer > Properties > Java Compiler > Compiler Compliance level : 1.8 – tozCSS Oct 15 '14 at 21:56
  • I am using Kepler and just updated to 1.8, although I am required to keep a source compliance at 1.7 for now. I'm getting hundreds of errors for this Comparator. My office mate is using Luna and doesn't see the problem, any ideas? – Batman0730 Aug 17 '15 at 20:00
9

The new methods in the Java 8 version of Comparator are default methods (a new feature in Java 8) which not only include the method signature, but also a default implementation. Thus, older Comparator implementations should work just fine on Java 8 if they worked before.

If something is not working, please let us know what you are trying and what the error message is.

Stuart Marks
  • 127,867
  • 37
  • 205
  • 259
  • I somehow overlooked the default keyword in the source. I can only assume it's an eclipse problem (i have installed the jdt and pde patches for java 8). – thobens Mar 22 '14 at 06:43
  • 2
    As I assumed it is an Eclipse config problem with different Java versions in the project properties and in the MANIFEST.MF file. I thought changing the Execution environment in the MANIFEST.MF would be enough to get Eclipse use Java 8 as source version. Thanks for your help! – thobens Mar 22 '14 at 07:00
0

that happen because for java 7 Comparator does not had all the other methods, when you update Eclipse to Java 1.8 then the new methods are needed in order to use Comparator, to solve this just in change the Compiler back to Java 1.7 ( properties - Java Compiler --- JDK Compliance ), refresh your project an that it.. hope this help

  • 1
    Just to clarify why I downvoted this answer: In the question, I explicitly stated that I'm migrating to Java 8, so setting the Compiler back to 1.7 is no option. I hope this helps improving your future answers. – thobens Nov 02 '15 at 14:10