List.sort() NoSuchMethodException 1.6 vs 1.8

Viewed 452

So i'm a little confused about a change in Java 8 - List.sort - bear with me as the confusion will become apparent.

I have Java 8 JDK installed and running Eclipse with the Project in question set to compile in 1.6 (Windows environment).

Throughout my code I've been doing (Example extends BaseExample):

public static final Comparator<BaseExample> sortByLevel_DESC = new Comparator<NavItemBase>() {...};

List<Example> examples = new ArrayList<Example>();

examples.sort(sortByLevel_DESC);

Despite compiling to 1.6, this worked, and has always worked for me (remember i have Java 8 installed).

However...

Since applying this code to a clients machine - with Java 7 (JRE not JDK) installed (Linux Environment), the exception "java.util.List.sort() NoSuchMethodException" is thrown.

Changing the code from: examples.sort(sortByLevel_DESC); to: Collections.sort(examples, sortByLevel_DESC); resolves the issue.

This leads me to the obvious conclusion that its due to the Java 8 specific code, and due to Java 8 not being installed, it falls over.

But i'm left wondering...

Why eclipse does not complain about Java 8 code when not compiled to Java 8 in the exact same way it will complain if you try to use Lambda expressions while not compiled to Java 8:

examples.stream().filter(e -> e.active()).collect(Collectors.toList());

(e -> e.active()) being the problem:

1.8 source

I would have thought if the method was java 8 only, and i was compiled to 6, then only java 6 code can be executed - I have actually relied on this "project specific setting" to to ensure i don't write any code that is not compatible with the lower version that clients often have, like when i tried the lambda expression.

Perhaps this is actually a problem with Eclipse and not Java? Or perhaps this is NOT the same as lambda expressions at all (in the way it should show an error)?

2 Answers
Related