1

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);
    }
}
Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111

1 Answers1

1

List.of() was introduced in Java 9. This indicates that your update from Java 8 to Java 11 wasn't successful.

Run ./gradlew -v to see which version of Java is resolved by Gradle:

------------------------------------------------------------
Gradle 5.3.1
------------------------------------------------------------

Build time:   2019-03-28 09:09:23 UTC
Revision:     f2fae6ba563cfb772c8bc35d31e43c59a5b620c3

Kotlin:       1.3.21
Groovy:       2.5.4
Ant:          Apache Ant(TM) version 1.9.13 compiled on July 10 2018
JVM:          11.0.2 (Oracle Corporation 11.0.2+9)
OS:           Linux 4.15.0-47-generic amd64

Make sure you aren't using Gradle daemon started on Java 8 before updating to Java 11 and in build.gradle force Java 11 compatibility:

sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111