Lambda expressions don't work in Java 8?

2019-01-25 06:03发布

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 ?

2条回答
够拽才男人
2楼-- · 2019-01-25 06:12

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.

查看更多
混吃等死
3楼-- · 2019-01-25 06:20

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.

查看更多
登录 后发表回答