1

I have problem with running following code:

    public class LambdaTesting {
        public static void main(String[] args){
             new LambdaTesting();    
        }
        public LambdaTesting(){
            test1();
        }
        private void test1(){
            Runnable x = () -> System.out.println("ok"); //error
        }
    }

which is causing following exception:

*Exception in thread "main" java.lang.IncompatibleClassChangeError 
at java.lang.invoke.MethodHandleNatives.linkMethodHandleConstant(MethodHandleNatives.java:383)
    at LambdaTesting.test1(LambdaTesting.java:24)
    at LambdaTesting.<init>(LambdaTesting.java:20)
    at LambdaTesting.main(LambdaTesting.java:15)
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:765)
    at java.lang.invoke.MemberName$Factory.resolveOrFail(MemberName.java:882)
    at java.lang.invoke.MethodHandles$Lookup.resolveOrFail(MethodHandles.java:1019)
    at java.lang.invoke.MethodHandles$Lookup.linkMethodHandleConstant(MethodHandles.java:1284)
    at java.lang.invoke.MethodHandleNatives.linkMethodHandleConstant(MethodHandleNatives.java:381)
    ... 3 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:854)
    at java.lang.invoke.MemberName$Factory.resolveOrFail(MemberName.java:879)
    ... 6 more*

I have installed jdk8 downloaded from: http://jdk8.java.net/lambda/ (Windows x64 version)

I run it in Eclipse Version: 4.4.0 downloaded from: http://downloads.efxclipse.org/eclipse-java8/2013-06-30/ (file: org.eclipse.sdk.ide-win32.win32.x86_64.zip 30-Jun-2013 17:35 180M)

eclipse.ini file:

-vm
C:\Program Files\Java\jre8\bin\java.exe
-startup
plugins/org.eclipse.equinox.launcher_1.3.0.v20130327-1440.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.200.v20130521-0416
-showsplash
org.eclipse.platform
--launcher.defaultAction
openFile
--launcher.appendVmargs
-vmargs
-Xms40m
-Xmx512m

Project properties -> Java Compiler -> Compiler compliance level: 1.8 (BETA) (Use default compliance settings is checked).

Project properties -> Java Build Path -> Libraries: JRE System Library [jre8]

Project Run Configurations:

[JRE tab] Project JRE (jre8) checked

[Classpath tab] Boostrap Entries: JRE System Library [jre8]

I have also tried to run LambdaTesting.class from command line inside jre8/bin directory but same exception appeared.

Java version:

C:\Program Files\Java\jre8\bin>java.exe -version
java version "1.8.0-ea"
Java(TM) SE Runtime Environment (build 1.8.0-ea-b102)

My operating system is: Windows 7 x64

Any clues ?

Thanks to @assylias i solved the problem of compilation from command line using jdk1.8.0/bin/javac.exe, but still no result in Eclipse. Seems that Eclipse has wrong compiler. I tried to change settings in: Preferences -> Java -> Compiler, but i can only choose version 1.7 in "Generated .class files compatibility" (no 1.8 available in this setting). "Use default compliance settings" causing above mentioned exception.

DominikStyp
  • 360
  • 6
  • 10
  • Your program compiles and runs fine for me (b100). Have you tried to compile it with javac from the command line? It seems to be an eclipse configuration issue. – assylias Aug 12 '13 at 22:09
  • 1
    @assylias. Yeah it works without braces. Thanks. :) – Rohit Jain Aug 12 '13 at 22:10
  • Eclipse doesn't show any errors, but if I run this code as Java Application it throws an exception. – DominikStyp Aug 12 '13 at 22:50
  • @assylias: Thanks, javac solved the problem of compilation, but i still can't compile code properly in Eclipse. Any clues for Eclipse? – DominikStyp Aug 12 '13 at 23:09
  • @user2676480 Nop - I've never liked eclipse ;-) You don't seem to be alone having troubles with eclipse+java8: http://stackoverflow.com/questions/17366615/java-8-hashmap-initialization-with-lambda-expressions – assylias Aug 12 '13 at 23:11
  • Considering that it is a Beta I would think it more than unfair to make any judgements about eclipse on that baisis – TechTrip Nov 14 '13 at 16:37

1 Answers1

0

I found that your example compiles fine for the latest beta. Not sure what you are trying to evaluate but I did a similar test as follows:

public class LambdaTesting {
    public static String[] strs = { "a", "aa", "aaa"};

    public static void main(String args[]) throws Exception {
        ExecutorService pool = Executors.newFixedThreadPool(3);
        Set<Future<Integer>> set = new HashSet<Future<Integer>>();
        for (String word : strs) {

            Callable<Integer> c = (() -> word.length());

            Future<Integer> future = pool.submit(c);
            set.add(future);
        }
        int sum = 0;
        for (Future<Integer> future : set) {
            sum += future.get();
        }
        System.out.printf("The sum of lengths is %s%n", sum);
        System.exit(sum);
    }
}

I used an example executor I found here: java Runnable run() method returning a value as my base program and substituted a lambda expression for the wordcount. This all worked fine with the latest Java 8 beta and a September 2013 build of eclipse Kepler as found here: http://downloads.efxclipse.org/eclipse-java8/ - Trip

Community
  • 1
  • 1
TechTrip
  • 4,299
  • 2
  • 21
  • 16