Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda expressions don't work in Java 8?

I have a virtual machine running Windows XP SP3 32-bit. On this machine I installed the Java SE JDK 8 build b44 Developer Preview from here.

I also installed the JavaFX 2.1 SDK.

It works fine:

java -version
> java version "1.8.0-ea"
> Java(TM) SE Runtime Environment (build 1.8.0-ea-b44)
> Java HotSpot(TM) Client VM (build 24.0-b14, mixed mode, sharing)

I tried running the following program (taken from here):

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.ButtonBase;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleButtonBuilder;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class LambdasWithJavaFx extends Application
{
    public static void main(String[] args)
    {
        Application.launch(args);
    }

    @Override  public void start(Stage stage) throws Exception
    {
        BorderPane root = new BorderPane();
        ToggleButton button = new ToggleButton("Click");
        final StringProperty btnText = button.textProperty();

        button.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override public void handle(ActionEvent actionEvent)
            {
                ToggleButton source = (ToggleButton) actionEvent.getSource();
                if (source.isSelected())
                {
                    btnText.set("Clicked!");
                }
                else
                {
                    btnText.set("Click!");
                }
            }
        });

        root.setCenter(button);
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.setWidth(200);
        stage.setHeight(200);
        stage.show();
    }
}

The program compiled and ran as expected.

I followed the instructions in that article and replaced the button event-handling code with this:

button.setOnAction((ActionEvent event)->
{
    ToggleButton source = (ToggleButton) event.getSource();
    if (source.isSelected())
    {
        btnText.set("Clicked!");
    }
    else
    {
        btnText.set("Click!");
    }
});

When compiling, I get the following error (on the line button.setOnAction((ActionEvent event)->):

> lambda expressions are not supported in -source 1.8
> (use -source 8 or higher to enable lambda expressions)

I added the argument -source 8, nothing changed.

All I wanted was to check the lambda expressions functionality in Java 8. Why doesn't it work ?

like image 263
Radu Murzea Avatar asked Jun 24 '12 11:06

Radu Murzea


People also ask

Is lambda expressions introduced in Java 8?

Introduction. Lambda expressions are a new and important feature included in Java SE 8. They provide a clear and concise way to represent one method interface using an expression. Lambda expressions also improve the Collection libraries making it easier to iterate through, filter, and extract data from a Collection .

Which of the following is correct about Java 8 lambda expression?

Explanation. Both of the above options are correct. Q 5 - Which of the following is correct about Java 8 lambda expression? A - Lambda expressions are used primarily to define inline implementation of a functional interface.

Is lambda expression introduced in Java 9?

Lambda expression is a new and important feature of Java which was included in Java SE 8. It provides a clear and concise way to represent one method interface using an expression.

How do you use 8 or higher to enable lambda expressions in Intellij?

Select project and press alt+ctrl+shift+s and you get Project Structure window where you need to set project sdk and language level for project. Then you should be able to use lambdas. This is exactly what I did with out success.


2 Answers

You need to download the binaries which contain the Lambda expressions feature. Try downloading from here http://jdk8.java.net/lambda/. I remember reading in the mailing list that the lambda expression branch is being merged in the main JDK8 build, but not sure if its been done. But I use the build from the Lambda project page.

like image 177
MohamedSanaulla Avatar answered Oct 11 '22 05:10

MohamedSanaulla


As an alternative to the http://jdk8.java.net/lambda/ releases, there are also JDK8 and JRE8 snapshot releases available in dmg, exe installer and tar.gz form from http://jdk8.java.net/download.html.

These jdk8 developer snapshots also now include lambda (and javafx8) functionality. As of this writing, both lambda and javafx for jdk8 are still in feature and bug fix development mode.

I have found the http://jdk8.java.net/download.html weekly builds useful and useable for development (programs using them will occasionally crash on my mac, so it's definitely not something you would want to deploy to production even if the oracle license for the builds permitted that, which they don't).

If you want the absolute latest bleeding edge or customized jdk builds, you can build your own using obuildfactory. Such builds use an OpenJDK licensing model rather than an Oracle JDK licensing model, which may be preferable for some development projects, though, personally, I stick with Oracle JDK builds when I can.

like image 37
jewelsea Avatar answered Oct 11 '22 05:10

jewelsea