我正在尝试学习 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]));
}
}