2

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

The method substring(int, int) is undefined for the type CharSequence

How can Eclipse interfere with the compiler? Doesn't an IDE just use javac.exe?

I'm using Eclipse Luna 4.4.1, with Oracle jdk1.8.0_45

Luigi Cortese
  • 10,841
  • 6
  • 37
  • 48
  • 2
    Works for me in Eclipse Mars 4.5.1. Just upgrade. – Sotirios Delimanolis Oct 16 '15 at 19:48
  • 10
    Is it possible that your Eclipse project uses a different version of Java for compilation? – brimborium Oct 16 '15 at 19:49
  • 7
    Possible duplicate of [What is the difference between javac and the Eclipse compiler?](http://stackoverflow.com/questions/3061654/what-is-the-difference-between-javac-and-the-eclipse-compiler) – Markus Patt Oct 16 '15 at 19:49
  • 1
    The general solution to your question is: understand the variables in the equation. In that case: what version of Java comes into play on the command line (something that knows Java 8) ... and what version of Java is supported by your eclipse (probably not Java 8; so guess you are running on Luna(?) or older?! – GhostCat Oct 16 '15 at 19:51
  • If the OP wasn't using Java 8, they'd have a lot more errors than the missing method, since it would also be unable to resolve Collectors, Stream, `->`, etc. – azurefrog Oct 16 '15 at 19:52
  • You may upgrade the jdk for your working [project](http://stackoverflow.com/questions/12588537/how-to-change-jdk-version-for-an-eclipse-project) – Razib Oct 16 '15 at 19:52
  • 2
    I can confirm this doesn't compile on Luna (4.4) with Java 8, but works on Mars. – Cinnam Oct 16 '15 at 19:53
  • Also, `CharSequence` doesn't have a `substring(int, int)` method, it's got `subSequence(int, int)`. I think the real question here is why x is being treated as a `CharSequence` and not a `String`. – azurefrog Oct 16 '15 at 19:54
  • This also compiles fine for Eclipse Mars 4.5.0. Seems this is only a Luna (or before) problem. – Tunaki Oct 16 '15 at 19:58
  • Tell us the version of Eclipse you use. – Marged Oct 16 '15 at 20:21
  • @Jägermeister I added Eclipse and jdk version – Luigi Cortese Oct 16 '15 at 22:24

2 Answers2

4

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.

enter image description here

enter image description here

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

Community
  • 1
  • 1
Marged
  • 10,577
  • 10
  • 57
  • 99
  • Already checked that, JRE is the right one. At this point, shouldn't this be considered a bug? – Luigi Cortese Oct 16 '15 at 22:29
  • 1
    As soon as you are sure this does not work on a clean install of Eclipse and that your code does not contain any errors. I had to change your code to `x -> ((String) x).substring(0,1)` in order to make it run on my STS 3.7.1 – Marged Oct 16 '15 at 22:32
  • That's it! You don't need any change to successfully compile it with Oracle javac.exe – Luigi Cortese Oct 16 '15 at 23:03
  • Perhaps you could create a new answer and ask if it is possible to achieve the same without this rather ugly cast. – Marged Oct 17 '15 at 07:28
  • 1
    To avoid the cast you can also declare the type of `x` like this: `(String x) -> x.substring(0,1)`. This will help the compiler while also preserving type safety. Nevertheless, this looks like a bug in the eclipse < 4.5 compiler. – Didier L Oct 17 '15 at 16:25
1

The problem has been resolved in Eclipse as of 4.5 Milestone 3, most likely via bug 437444. That change combines some bug fixes with incorporating several JLS changes made after the release of Java 8.

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.

Stephan Herrmann
  • 7,963
  • 2
  • 27
  • 38