3

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

    }
}

enter image description here

7
  • Did you try this on NetBeans? Also, try to clean and build the project. Commented Oct 3, 2013 at 10:35
  • No I have not tried it on Netbeans Commented Oct 3, 2013 at 10:39
  • 1
    Try it there. Eclipse is not yet completely stable for use with Java 8. Also try just to compile from command line, or terminal whatever you have. Commented Oct 3, 2013 at 10:41
  • you are right, eclipse has problem in supporting java8. I have successfully compiled and runned on command prompt. Commented Oct 3, 2013 at 10:52
  • FYI, Java 8 support on Netbeans is already really good, including very useful hints to auto-create lambda code. Commented Oct 3, 2013 at 17:08

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

5 Comments

this is most likely a problem with the eclipse compiler, and the java8 runtime. just use the jdk8 compiler and runtime and you will be fine.
As you may see that I am pointing jdk8 at eclipse, it indicates that there is something wrong cases not to run application properly
eclipse has a separate compiler it uses, it does not use the compiler included in the jdk.
I don't think so, since I can change the JDK version,let say using 1.7,1.6 where lambda expression is not known.
that is a language level setting. eclipse just passes that setting into its own internal compiler. if you want eclipse to use the external compiler, use ant ant script, or a custom build step.
0

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> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.