Java 8 Lambdas don't work, everything else fro

2019-02-07 20:10发布

问题:

I found out that Java 8 is officially released now. It seems that I need Eclipse Luna 4.4 for it to work. So I downloaded Luna and installed it. I also imported all my projects from my other Eclipse, everything worked as expected. When I wanted to try Java 8, I quickly found out I needed to install it first. After I installed it I managed to change JRE 8 to the default.

My question is: Why can I use the new date & time API, but I cannot use the new Lambda Expressions?

Some info that might be useful:

  • I'm using a Mac
  • I'm very certain that the time API works (Even the small Java Doc Box says it's since 1.8)

Could it have something to do with the fact that I didn't install Eclipse with Java 8 included?

回答1:

Firstly, you don't need to use Luna - there's a feature patch for Kepler which works fine.

Secondly, the "source compatibility" part of the Java Compiler dialog has to be 1.8. Otherwise even though you're allowed to use the library features of Java 1.8, you won't be able to use the language features. (It's not just lambdas - there's method references, static methods in interfaces, and default methods for example.) Here's where to look:

It would be rare that you'd want to use library features from 1.8 but keep source/classfile compatibility with 1.7 or earlier, but I guess it could be useful if you were writing code that needed to run on various JREs, but you could have some feature implementations which required Java 1.8 and just wouldn't be used on earlier JREs.