Andrew Medhurst
Andrew Medhurst

Reputation: 133

Cannot set java.home in VS Code Maven project

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://stackoverflow.com/questions/52539414/i-cant-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.

Upvotes: 7

Views: 6837

Answers (1)

mondaka
mondaka

Reputation: 374

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

Upvotes: 6

Related Questions