List.sort () NoSuchMethodException 1.6 vs 1.8

So, I'm a little confused about changing Java 8 - List.sort - bear with me, as the confusion will become apparent.

I have a Java 8 JDK installed and running Eclipse with a given project to compile in version 1.6 (Windows environment).

Throughout my code that I have been doing (the 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, it worked and always worked for me (remember that I have Java 8 installed).

But...

From the moment this code is applied to the client 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); before: Collections.sort(examples, sortByLevel_DESC); fixes the problem.

This leads me to the obvious conclusion that it crashes because of the specific code of Java 8 and because Java 8 is not installed.

But I'm not interested ...

Why does eclipse not complain about Java 8 code when it is not compiled in Java 8 in the same way as it will complain if you try to use lambda expressions rather than compiled in Java 8:

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

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

1.8 source

I would think if the method was only java 8, and I was compiled to 6, then only java 6 code can be executed - I really relied on this “specific project” to ensure that I don’t write any code that is incompatible with the lower version that clients often have, for example, when I tried the lambda expression.

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

+5
source share
2 answers

The problem is, of course, related to the settings in eclipse, follow the instructions below to avoid the problems you are facing.

In the menu bar: Project → Properties → Java Compiler

Enable special project settings (checked) Uncheck "Use compliance with runtime requirements" .... Select the desired "compiler compliance level"

This will allow you to compile the code "1.6" using the JDK "1.8".

If you want to use the 1.6 JDK to create a "1.6" -compatible code, then install the appropriate 1.6 JDK and tell the eclipse where it is installed via:

Window → Settings → Installed JRE

And then go back to the project

Project -> Properties -> Java Build Path -> Libraries

delete system libraries 1.8 and: add library ... → JRE System LIbrary → Alternate JRE → JRE you want.

Make sure that the correct JRE is on the way to build the project, save everything and enjoy!

Link: how to get eclipse to use a different version of the compiler for Java?

+1
source

When it comes to developing methods that a class or interface uses Eclipse, it simply uses the runtime banners in any JRE (or JDK) that you specified for the project in the Java Build Path.

So, to verify that your code compiles with Java 6, you need to tell Eclipse about the Java 6 JRE in the settings in Java> Installed JREs. Then you use this JRE for the project (on the Library tab, “Java Build Path” in the project properties).

Note that changing the runtime to Java 1.6 is not enough unless you have the "perfect combination" JRE / JDK installed.

+3
source

Source: https://habr.com/ru/post/1268360/


All Articles