简体   繁体   中英

IncompatibleClassChangeError in Java 8 (Lambda expression)

I am trying to learn lambda expression so following http://www.oracle.com/technetwork/articles/java/lambda-1984522.html . Downloaded following IDE and JDK

JDK 8 Eclipse IDE with JDK 8 support

But When after compiled following code, I had an exception

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)

Simple code that I am trying to execute.

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]));

    }
}

在此输入图像描述

eclipse has problem in supporting java8. I have successfully compiled and runned on command prompt

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 Hello 12 12

Add in your pom.xml, if you're using Maven

  <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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM