I have some Java code that compiles on my Mac, but fails to compile on my Ubuntu machine. Specifically, when using the static of
method of List
, I get the error:
error: cannot find symbol
List<String> list2 = List.of("cat", "dog");
^
symbol: method of(String,String)
location: interface List
1 error
I thought perhaps this was an issue with the Java version. Sure enough, it was using only Java 8. However, I subsequently upgraded my Ubuntu machine to use Java 11 using OpenJDK, but I still get the same issue.
The output I get from running java -version
on the Ubuntu machine is:
openjdk version "11.0.1" 2018-10-16
OpenJDK Runtime Environment (build 11.0.1+13-Ubuntu-3ubuntu116.04ppa1)
OpenJDK 64-Bit Server VM (build 11.0.1+13-Ubuntu-3ubuntu116.04ppa1, mixed mode, sharing)
The output I get from running java -version
on the Mac machine is:
java version "10.0.1" 2018-04-17
Java(TM) SE Runtime Environment 18.3 (build 10.0.1+10)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.1+10, mixed mode)
For compilation, I am using Gradle version 5.2.1. I am using the Gradle wrapper, so both machines are using the same version of Gradle.
Here is the file that does not compile properly.
package mypackage;
import java.util.List;
import java.util.ArrayList;
public class App {
public static void main(String[] args) {
// No errors on both MacOS and Ubuntu
List<String> list1 = new ArrayList<>();
list1.add("cat");
System.out.println(list1);
// Fails to compile on Ubuntu
List<String> list2 = List.of("cat", "dog");
System.out.println(list2);
}
}