3

I wanted to learn the differences between Java 8 and Java 7. At the moment I am looking into the Java 8 forEach loop.

public class Main {
    public static void main(String[] args){
        List<String> strings = Arrays.asList("hello","my","name","is","test");
        strings.stream().forEach(s -> System.out.println(s));
    }
}

This code is generating the following error for me:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
s cannot be resolved to a variable
Syntax error on token "-", -- expected
s cannot be resolved to a variable

at Main.main(Main.java:7)

I am sure that the JDK aswell as the JRE are Java 8, since the compuler can recognize the forEach method from the List but not the -> parameter.

4
  • This will give you an idea adam-bien.com/roller/abien/entry/java_8_from_a_for Commented Dec 30, 2014 at 20:18
  • It sounds like a Java 7 vs Java 8 bug. What is the output from your program if you run System.out.println(System.getProperty("java.version"));? Commented Dec 30, 2014 at 20:31
  • I believe I know what is happening. The JRE is 1.8.0_25 as output by System.getProperty(). However, the JDK is 1.7. God knows where Eclipse has that setting from though. Commented Dec 30, 2014 at 20:33
  • Good, issue resolved then ;) Commented Dec 30, 2014 at 20:35

2 Answers 2

2

I fixed this problem by reinstalling the JRE, JDK and created a completely new installation of eclipse. Apparently the old eclipse was still linked to some leftovers from a Java 7 JDK.

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

Comments

0

Test you java version in command prompt c:> java -version

If it gives 1.7 then your JDK is not updated to 1.8.

In eclipse you can windows->preferences->java->compiler and select compiler compliance level as 1.8

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.