Java 8 Lambdas don't work, everything else from Java 8 works though
Asked Answered
H

1

29

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?

Hamill answered 20/3, 2014 at 20:12 Comment(2)
Set your JDK compliance level to 8.Terresaterrestrial
You don't need to use Luna - there's a feature patch for Kepler which works fine.Farriery
F
67

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:

Java 1.8 compiler settings

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.

Farriery answered 20/3, 2014 at 20:17 Comment(6)
Okay thank you, I didn't quiet realize that Eclipse Luna wasn't really supposed to be used.Hamill
@Kametrixom: Well you can use Luna if you want; it's just not the latest released version. Somewhat beta-ish, although likely to be fine.Farriery
@JonSkeet Weren't static methods in interfaces dropped from Java 8?Triennium
This worked out for me. I spent some time looking into the jvm mentioned in my eclipse.ini that was 1.8 . There was no need to download the lambda libraries mentioned here: #11177436Twentyfour
My group is currently in the state where we use Java 1.8 libraries but retain 1.7 code compatibility. The reason is that we are stuck with code compatibility 1.7 is that we chose a toolset that relies on a custom version of Eclipse that is no longer updated (Adobe Flashbuilder). If we were to take a project built with 1.8 semantics and open it in flashbuilder, it just show the code as full of errors. There are various solutions (We are progressing towards some slowly) but I thought it worth mentioning that there is at least one case! PS, I use all the new libraries, just not closure syntax.Nicollenicolson
@BillK: I hope you have a plan to move away from FlashBuilder then. Being stuck in a dead-end tool chain is going to get nastier and nastier for you...Farriery

© 2022 - 2024 — McMap. All rights reserved.