简体   繁体   English

为什么我可以通过命令行而不是Eclipse来编译此代码?

[英]Why can I compile this code via command line but not with Eclipse?

These lines of code 这些代码行

    System.out.println(
        Stream.of("adam", "bob", "adrian", "brian")
        .collect(
                Collectors.groupingBy(
                        x -> x.substring(0,1),        //1
                        Collectors.joining("-")))
    );

compile fine when using javac via command line, but when I try to compile in Eclipse I get this error 通过命令行使用javac时可以很好地进行javac ,但是当我尝试在Eclipse中进行编译时,出现此错误

The method substring(int, int) is undefined for the type CharSequence 未为CharSequence类型定义方法substring(int,int)

How can Eclipse interfere with the compiler? Eclipse如何干扰编译器? Doesn't an IDE just use javac.exe ? IDE不只是使用javac.exe吗?

I'm using Eclipse Luna 4.4.1, with Oracle jdk1.8.0_45 我正在使用带有Oracle jdk1.8.0_45的Eclipse Luna 4.4.1

In the first place you should make sure that this isn't a syntax error. 首先,您应该确保这不是语法错误。 Change your code to look like this: 更改您的代码,如下所示:

x -> ((String) x).substring(0,1)

Besides making sure that you use a fully Java 8 compatible version of Eclipse, you should check if you allow Eclipse to use Java 8 language features. 除了确保使用完全兼容Java 8的Eclipse版本外,还应检查是否允许Eclipse使用Java 8语言功能。

在此处输入图片说明

在此处输入图片说明

If you didn't specify the correct version of JRE you can change it like described here: How to change JDK version for an Eclipse project 如果未指定正确的JRE版本,则可以按以下说明进行更改: 如何为Eclipse项目更改JDK版本

The problem has been resolved in Eclipse as of 4.5 Milestone 3, most likely via bug 437444 . 从4.5 Milestone 3开始,该问题已在Eclipse中解决,很可能是通过错误437444来解决的 That change combines some bug fixes with incorporating several JLS changes made after the release of Java 8. 该更改结合了一些错误修复, 并结合了 Java 8发布后进行的一些JLS更改

We could discuss whether the example was actually legal Java 8 before the spec changes (previously inference wasn't allowed to use some information from nested lambda bodies), but I hope just updating to Eclipse 4.5.1 will make everybody happy. 我们可以讨论在规范更改之前该示例是否实际上是合法的Java 8(以前不允许推断使用嵌套的lambda主体中的某些信息),但是我希望仅将其更新到Eclipse 4.5.1会使所有人满意。

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

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