10

I have a spring-boot project and my IDE is VS code. I want to pass an environment variable to my applications. Right now I set it before the Gradle command

export PROJECT_NAME=test

./gradlew bootrun

PROJECT_NAME is my env variable and I access this in application.properties

what is the recommended approach to set environment variables in VS code for java

Mahesh Madushanka
  • 2,902
  • 2
  • 14
  • 28
  • String variable = System.getProperty("mykey"); would get an environment var in Java.. https://stackoverflow.com/questions/1672281/environment-variables-for-java-installation Not sure if that is what you are after.. Also this https://code.visualstudio.com/docs/getstarted/settings – JGFMK Jan 10 '20 at 19:38
  • @JGFMK thanks for your input. the issue is I can't send env variables into java app from vs code. No issue with accessing the value from java code. With the VS code settings, we can set generic environment variables but not sure about custom parameters – Mahesh Madushanka Jan 10 '20 at 21:12
  • 1
    @MaheshMadushanka Consider accepting MinhTC's answer. That's the recommended way to do it with VSCode – GabrielBB Feb 29 '20 at 21:25

3 Answers3

21

In order to set environment variable for Spring boot application in VSCode, the recommended way is to create a launch.json file in .vscode folder of your project, then add the "env" section like the example below:

{
  "configurations": [
    {
      "type": "java",
      "name": "Spring Boot-DemoApplication<demo>",
      "request": "launch",
      "cwd": "${workspaceFolder}",
      "console": "internalConsole",
      "mainClass": "com.example.demo.DemoApplication",
      "projectName": "demo",
      "args": "",
      "env": {
        "PROJECT_NAME": "FOO_PROJECT"
      }
    }
  ]
}
MinhTC
  • 327
  • 3
  • 6
  • 2
    What to do, when one needs to set a password via the args? I don't want to commit the `build.json` with my password in it. But I want my other colleagues to have access the config file. – winklerrr Jan 20 '21 at 12:39
  • @winklerrr then you could add this launch.json in the project's .gitignore, and ask all of your colleagues to create an example of launch.json file locally, with their custom settings there :) – montie Jul 31 '23 at 14:11
3

I came across the same problem while trying to run JUNIT tests with customized environment variables. The above mentioned answer didn't help me. Instead, according to this documentation you have to create an env object in the setting.json file. After doing that I could query and get the customized env variables for running these tests.

{
    "java.semanticHighlighting.enabled": true,
    "window.zoomLevel": 0,
    "editor.suggestSelection": "first",
    "vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
    "java.requirements.JDK11Warning": false,
    "http.proxyAuthorization": null,
    "java.test.config": {
        "name": "ENVIRONMENT_JSON",
        "workingDirectory": "${workspaceFolder}",
        "env": {
            "CF_ORG": "testOrg",
            "CF_SPACE": "testSpace", 
            ....
            ....
        }
    }
}
s33h
  • 183
  • 1
  • 8
2

If you are using the following version of VSCode as you can see Help -> About,

Version: 1.60.2 (user setup)
Commit: 7f6ab5485bbc008386c4386d08766667e155244e
Date: 2021-09-22T12:00:31.514Z
Electron: 13.1.8
Chrome: 91.0.4472.164
Node.js: 14.16.0
V8: 9.1.269.39-electron.0
OS: Windows_NT x64 10.0.18363

Simply go to the Run menu and click on Open Configurations, the image is given below.

enter image description here

This will open launch.json, now you can add env details specific to your environment details. The example is given below.

{
    "configurations": [

        {
            "type": "java",
            "name": "Spring-Boot-App",
            "request": "launch",
            "cwd": "${workspaceFolder}",
            "console": "internalConsole",
            "mainClass": "com.blr.appName.ApplicationName",
            "projectName": "projectName",
            "args": "",
            "env": {
                "PROJECT_NAME": "FOO_PROJECT",
                "licenseKeyDetails":"license_details",
                "serialNumber":"ABCDEFG"
                 }
        }
    ]
}
Sambit
  • 7,625
  • 7
  • 34
  • 65