JDK 19 not being recognised in VSCode?

1.6k Views Asked by At

I have a grade project (7.6) that is also a LibGDX project and the latest java jdk (19.0.1) installed. I am on Mac OS Monterey btw.
The problem is that when opening the gradle project inside VSCode it throws a lot of errors.
It is however possible to run the program using the gradle run command.
Here are some examples of the errors that it throws:

Method references are allowed only at source level 1.8 or above
Syntax error on token "record", @ expected
Syntax error on token "double", { expected
Syntax error on tokens, ClassHeader expected instead
Syntax error on tokens, ClassInstanceCreationExpressionName expected instead

Here are the things that I have already tried to fix it:

  1. Set the JAVA_HOME variable to /Library/Java/JavaVirtualMachines/jdk-19.jdk/Contents/Home
  2. Set java.jdt.ls.java.home to /Library/Java/JavaVirtualMachines/jdk-19.jdk/Contents/Home in the user and workspace section of the VSCode settings
  3. Switch the "Language Support for Java(TM) by Red Hat" VSCode extension to the pre-release version
  4. Clean and reload the Java Language Server Workspace

Unfortunately without any success. At this point I'm clueless as to what else to try because it does work without problems with gradle, it's just inside VSCode that it shows all these errors.
Thank you very much in advance!

1

There are 1 best solutions below

2
On

Configure the java.configuration.runtimes property in your user settings.

Press ⌘ + ⇧ + P (Command + Shift + P) and select Preferences: Open User Settings (JSON) from the list. Insert the following settings between the first { and the last }.

"java.configuration.runtimes": [
  {
    "name": "JavaSE-1.8",
    "path": "/path/to/jdk-8",
  },
  {
    "name": "JavaSE-11",
    "path": "/path/to/jdk-11",
  },
  {
    "name": "JavaSE-19",
    "path": "/path/to/jdk-19",
    "default": true
  },
]

If you do not have multiple versions of Java in your system, remove all but the last one (i.e. jdk-19). In any case, make sure "default": true is there for jdk-19.

Alternatively,

Press ⌘ + ⇧ + P and select Java: Configure Java Runtime from the list. Edit the value of Java Version on the resulting page.