javagradlevisual-studio-codeenvironment-variables

How to set JAVA environment variable in VS Code


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; I access this in application.properties.

What is the recommended approach to set environment variables in VS Code for Java?


Solution

  • To set an environment variable for the Spring Boot application in VSCode, the recommended way is to create a launch.json file in the .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"
          }
        }
      ]
    }