Java compiler chokes on List.of

Java compiler chokes on List.of

by Peter Sander -
Number of replies: 1

Hello,

The following Java code:

import java.util.Arrays;
import java.util.List;

class Main {
   private String str = "";
   void print(List<String> l) {
       l.forEach(s -> str += s);
       System.out.println(str);
   }
}

works fine on my laptop with both of the following expressions

new Main().print(Arrays.asList("easy as", " 1", " 2", " 3"));
new Main().print(List.of("easy as", " 1", " 2", " 3"))
Both output easy as 1 2 3
My compiler is javac 11.0.4 and the "java.class.version" property is 55.0.


On CodeRunner with Question type set to java_class, I submit the Main class code as an Answer and the expressions as Tests. The first expression yields the expected result. The second expression (using List.of) upsets the compiler which complains that

Syntax Error(s)
__tester__.java:21: error: cannot find symbol
        new Main().print(List.of("easy as", " 1", " 2", " 3"));
                             ^
  symbol:   method of(String,String,String,String)
  location: interface List
1 error
In CodeRunner, the "java.class.version" property is again 55.0.
Putting an expression with List.of into the Answer produces the same
unfriendly result. As does List<String>.of.
Any ideas of what's going wrong?


In reply to Peter Sander

Re: Java compiler chokes on List.of

by Peter Sander -

Answering my own question...

Upon further investigation (meaning I hadn't done enough before posting the question :^( ), it turns out that the obvious answer was the right one. Our jobe server had been installed with different versions of the JDK (java 8) and the JRE (java 11). Upgrading the JDK solved the problem.