Cannot set java.home in VS Code Maven project
Asked Answered
M

1

7

I've looked everywhere. I've tried following

this guide: https://medium.com/@tariqul.islam.rony/learning-java-and-spring-boot-with-visual-studio-code-vscode-part-1-54073f2fa264

this post: https://mcmap.net/q/971540/-i-can-39-t-set-up-jdk-on-visual-studio-code/52541654#:~:text=In%20case%20you%20are%20on%20an%20older%20version%20of%20vscode,the%20path%20in%20%22%20%22%20).

And a host of other references that all say the same thing: Add "java.home" to your settings.json file, with the path to your jdk. And I have done just that.

java.home set in User settings.json

I have tried adding java.home to both the User level and Workspace level of the settings.json file. However, when I go to build my project with "mvn clean install -Dmaven.test.skip=true", it is still pointing at a JRE, and subsequently fails.

JRE Failure Message

Also, I found references to this so-called "Configure Java Runtime" wizard (hardly a wizard, since I can't change anything), that detects my different JDKs. However, I can't seem to select anything, or change the priority of things under the "Detected JDKs" section.

Configure Java Runtime "wizard"

Yes, setting JAVA_HOME as an environment variable (Windows 10) to the appropriate path works. But I support many different projects that force me to swap between different JDK versions. It would nice to be able to store this information within the projects themselves, rather than having to remember when to swap the JAVA_HOME value manually every time.

Thanks in advance for the assistance.

Mcgrew answered 25/2, 2021 at 16:48 Comment(3)
The java.home is just a setting pointing to the JDK to launch the Java Language Server. According to the screenshot for the maven execution error, it seems like you are running maven command in terminal. That honors your system environment not the VS Code.Bleeder
@ShengChen Interesting. Haven't heard that explanation before. So, is there some way to get my maven project to build through VS Code that I'm just not aware of? That way it will honor the JDK annotated in settings.json. I can't seem to find a section of the UI that would suggest that it performs such a function.Mcgrew
Take a look at this: github.com/redhat-developer/vscode-java#project-jdks. After this setting is well configured, the Java Language Server will automatically pick a suitable JDK according to your pom.xml's configuration.Bleeder
U
6

This is how I do it. Add this to your vscode settings:

    "maven.terminal.customEnv": [
    {
        "environmentVariable": "JAVA_HOME",                
        "value": "/path/to/jdk
    }
]    

Yes, this is adding a JAVA_HOME env variable, but only for maven terminals.

Of course, I assume you have installed the maven for java plugin: https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-maven

You can view a full configuration reference here:

https://github.com/Microsoft/vscode-maven

Unorthodox answered 16/12, 2021 at 16:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.