Himanshu
Himanshu

Reputation: 21

VSCode + maven project wrong java version

JAVA_HOME:

PS C:\Users\Himanshu\Desktop\work\demo> $Env:JAVA_HOME
C:\Program Files\Java\jdk-17.0.1

Java version mismatch

Please see the above image. Why is it displaying Java 1.6 and in brackets pointing to java 17 ? This is also not compiling lambda functions.

Looks like a bug in VSCode.

EDIT: I switched to Intellij Idea. And it worked smoothly. It is using the JAVA_HOME. :)

EDIT2: As asked in the comments

PS C:\Windows\System32> (Get-Command java.exe).Path
C:\Program Files\Common Files\Oracle\Java\javapath\java.exe

PS C:\Windows\System32> java --version
java 17.0.1 2021-10-19 LTS
Java(TM) SE Runtime Environment (build 17.0.1+12-LTS-39)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.1+12-LTS-39, mixed mode, sharing)

Upvotes: 2

Views: 4070

Answers (1)

xerx593
xerx593

Reputation: 13299

Ctrl+Shift+P, type (with auto-completion): "Java: Configure Runtime" (per project;)

VS Code screenshot


When "editing java version" (the pen symbol next to 17), I get redirected to pom.xml>project>properties>java.version (in this particular project):

 <properties>
    ...
    <java.version>17</java.version>
    ...
 </properties>

When, I delete/comment the <java.version> (java.version works only in poms, which mapped it (explicitly, like spring-booot-parent, in my example)) to:

  <properties>
    <maven.compiler.source>${java.version}</maven.compiler.source>
    <maven.compiler.target>${java.version}</maven.compiler.target>
  </properties>

(Ref)


... the maven default (when omitting these properties/not configuring compiler-plugin) is: 1.5!!!

Which results in (my VS Code to) the "closest matching" Execution Environment (in my case "JavaSE-1.8"):

Fallback to "closest (java)execution environment"


..., which is configured in "Settings>Extensions>Java>Configuration>Runtimes (edit in settings.json)", like:

...
   "java.configuration.runtimes": [        
        {
            "name": "JavaSE-1.8",
            "path": "C:\\Program Files\\Java\\jdk1.8.0_311"
        },
        {
            "name": "JavaSE-11",
            "path": "C:\\Program Files\\Java\\jdk-11.0.12"
        },
        {
            "name": "JavaSE-16",
            "path": "C:\\Program Files\\Java\\jdk-16.0.2"
        },
        {
            "name": "JavaSE-17",
            "path": "C:\\Program Files\\Java\\jdk-17"
        }
    ],
...
  • Bringing consistence in these settings
  • updating to latest JDK (minor) updates
  • and: Having distinct java.exe in %PATH%! (consistent with JAVA_HOME)

should solve our "Java Runtime Problems" (with the Official Java Extension).


But please keep in mind, that other extension (can) have custom "java runtime/jdk" configuration. I currently think of:

  • sonarlint
  • gradle

(Settings>Search Settings>"java...")

Upvotes: 1

Related Questions