Why the Error : java: cannot find symbol?

Context

I was working on a Maven project where I had to use the Maven Compiler Plugin for some requirements.

Here is the environment setup I used:

JDK: 8

Maven Compiler Plugin: 3.3

Below is my Maven Compiler Plugin configuration:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

If you notice, I was using the source and target for the same JDK version that I had installed on the machine.

However, during the work, I happened to upgrade my JDK to 17 for the obvious reason, JDK 8 being outdated and not supported officially

So I went ahead and installed JDK 17 in my machine and updated my project JDK from 8 to 17. I use IntelliJ and the settings looks something like this , except that this image is taken after I upgraded.

Problem

After I upgraded and updated JDK in the project as shown in the above image, I tried to run some code that used collections like Set.of() and List.of() , which are introduced in JDK 9.

When I wrote the code, I did not get any error in IDE but when I ran the program, I stumbled upon the below error:

error: cannot find symbol = List.of("A", "B", "C", "D", ^ symbol: method of(String,String,String,String,String,String,String,String) location: interface List

For a moment I was like, confused as to why this error, even after my project us updated with the right JDK version and the code support.

Root Cause

The error was due to Maven Compiler Plugin which was causing the issue.

I was still using source and target to compile with the older version of 1.8 instead of 17. So the JDK was still compiling my code to be compatible with JDK 8 hence the error.

Solution

I went ahead and fixed the source and target to align with JDK 17 as below and it fixed the issue.

And of course, we need to set the language level in the IntelliJ module setting to 9 or above, to support the code I wrote.

Sometimes we forget to think about the obvious and end up figuring out the error.

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>17</source>
                    <target>17</target>
                </configuration>
            </plugin>

Did you face this error or something similar ?