简体   繁体   English

List.sort()NoSuchMethodException 1.6 vs 1.8

[英]List.sort() NoSuchMethodException 1.6 vs 1.8

So i'm a little confused about a change in Java 8 - List.sort - bear with me as the confusion will become apparent. 所以我对Java 8的变化感到有点困惑 - List.sort - 请跟我一起,因为混乱会变得明显。

I have Java 8 JDK installed and running Eclipse with the Project in question set to compile in 1.6 (Windows environment). 我安装了Java 8 JDK并运行Eclipse,并将问题项设置为在1.6(Windows环境)中编译。

Throughout my code I've been doing (Example extends BaseExample): 在我的代码中,我一直在做(示例扩展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). 尽管编译为1.6,但这仍然有效,并且一直对我有用(记得我安装了Java 8)。

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. 由于将此代码应用于客户端计算机 - 安装了Java 7(JRE而非JDK)(Linux环境),因此抛出异常“java.util.List.sort()NoSuchMethodException”。

Changing the code from: examples.sort(sortByLevel_DESC); 更改代码: examples.sort(sortByLevel_DESC); to: Collections.sort(examples, 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. 这使我得出了一个明显的结论,即由于Java 8特定的代码,并且由于Java 8没有安装,它会失败。

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: 为什么eclipse不会在没有编译到Java 8时抱怨Java 8代码,就像你在没有编译到Java 8时尝试使用Lambda表达式一样抱怨它:

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

(e -> e.active()) being the problem: (e - > e.active())成为问题:

1.8来源

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. 我想如果方法只是java 8,并且我被编译为6,那么只能执行java 6代码 - 我实际上依赖于这个“项目特定设置”来确保我不写任何代码与客户端通常拥有的较低版本不兼容,例如我尝试使用lambda表达式时。

Perhaps this is actually a problem with Eclipse and not Java? 也许这实际上是Eclipse而不是Java的问题? Or perhaps this is NOT the same as lambda expressions at all (in the way it should show an error)? 或者这可能与lambda表达式完全不同(它应该显示错误的方式)?

When it comes to working out what methods a class or interface has Eclipse just uses the runtime jars in whatever JRE (or JDK) you have specified for the project in the Java Build Path. 在计算类或接口具有哪些方法时,Eclipse只使用您在Java Build Path中为项目指定的任何JRE(或JDK)中的运行时jar。

So to check that your code compiles with Java 6 you need to tell Eclipse about a Java 6 JRE in the Preferences in 'Java > Installed JREs'. 因此,要检查您的代码是否使用Java 6进行编译,您需要在“Java>已安装的JRE”的“首选项”中告知Eclipse有关Java 6 JRE的信息。 You then use that JRE for the project (in the 'Library' tab of 'Java Build Path' in the project Properties). 然后,将该JRE用于项目(在项目属性中的“Java Build Path”的“Library”选项卡中)。

Note that changing the execution environment to Java 1.6 is not enough unless you have an 'perfect match' JRE/JDK installed. 请注意,除非您安装了“完美匹配”JRE / JDK,否则将执行环境更改为Java 1.6是不够的。

The issue is definitely with the settings in eclipse, please follow the steps mentioned below to avoid the problem you are facing. 问题肯定是eclipse中的设置,请按照下面提到的步骤来避免您遇到的问题。

From the menu bar: Project -> Properties -> Java Compiler 从菜单栏:Project - > Properties - > Java Compiler

Enable project specific settings (checked) Uncheck "use Compliance from execution environment '.... Select the desired "compiler compliance level" 启用项目特定设置(选中)取消选中“使用执行环境中的合规性”....选择所需的“编译器合规级别”

That will allow you to compile "1.6" code using a "1.8" JDK. 这将允许您使用“1.8”JDK编译“1.6”代码。

If you want to acutally use a 1.6 JDK to produce "1.6" compliant code, then install a suitable 1.6 JDK and tell eclipse where it is installed via: 如果你想实际使用1.6 JDK生成“1.6”兼容代码,那么安装一个合适的1.6 JDK并告诉eclipse安装它的位置:

Window -> preferences -> Installed JREs 窗口 - >首选项 - >已安装的JRE

And then go back to your project 然后回到你的项目

Project -> properties -> Java Build Path -> libraries 项目 - >属性 - > Java构建路径 - >库

remove the 1.8 system libaries, and: add library... -> JRE System LIbrary -> Alternate JRE -> The JRE you want. 删除1.8系统库,并:添加库... - > JRE System LIbrary - > Alternate JRE - >你想要的JRE。

Verify that the correct JRE is on the project's build path, save everything, and enjoy! 验证项目的构建路径上是否有正确的JRE,保存所有内容并享受!

Reference : how do I get eclipse to use a different compiler version for Java? 参考: 如何让eclipse使用不同的Java编译器版本?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM