3

我正在尝试学习 lambda 表达式,因此请遵循http://www.oracle.com/technetwork/articles/java/lambda-1984522.html。下载以下IDE和JDK

JDK 8 Eclipse IDE,支持 JDK 8

但是当编译以下代码后,我有一个异常

Exception in thread "main" java.lang.IncompatibleClassChangeError
    at java.lang.invoke.MethodHandleNatives.linkMethodHandleConstant(MethodHandleNatives.java:444)
    at com.ahmetk.java8.Hello.main(Hello.java:10)
Caused by: java.lang.NoSuchMethodException: no such method: java.lang.invoke.LambdaMetafactory.metaFactory(Lookup,String,MethodType,MethodHandle,MethodHandle,MethodType)CallSite/invokeStatic
    at java.lang.invoke.MemberName.makeAccessException(MemberName.java:800)
    at java.lang.invoke.MemberName$Factory.resolveOrFail(MemberName.java:917)
    at java.lang.invoke.MethodHandles$Lookup.resolveOrFail(MethodHandles.java:1101)
    at java.lang.invoke.MethodHandles$Lookup.linkMethodHandleConstant(MethodHandles.java:1363)
    at java.lang.invoke.MethodHandleNatives.linkMethodHandleConstant(MethodHandleNatives.java:442)
    ... 1 more
Caused by: java.lang.NoSuchMethodError: java.lang.invoke.LambdaMetafactory.metaFactory(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;
    at java.lang.invoke.MethodHandleNatives.resolve(Native Method)
    at java.lang.invoke.MemberName$Factory.resolve(MemberName.java:889)
    at java.lang.invoke.MemberName$Factory.resolveOrFail(MemberName.java:914)

我试图执行的简单代码。

public class Hello {
    interface HelloService {
        String hello(String firstname, String lastname);
    }

    public static void main(String[] args) {

        HelloService helloService = (String firstname, String lastname) -> {
            String hello = "Hello " + firstname + " " + lastname;
            return hello;
        };
        System.out.println(helloService.hello(args[0], args[1]));

    }
}

在此处输入图像描述

4

2 回答 2

1

eclipse 在支持 java8 方面存在问题。我已成功编译并在命令提示符下运行

D:\tools\java\jdk1.8.0_32Bit\bin\javac com/ahmetk/java8/Hello.java

D:\tools\java\jdk1.8.0_32Bit\bin\java -cp 。com.ahmetk.java8.Hello 12 12 你好 12 12

于 2013-10-03T10:53:28.827 回答
0

如果您使用 Maven,请添加您的 pom.xml

  <build>  
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
  </build> 
于 2018-09-10T13:51:26.537 回答