Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 552
Mahesh Madushanka Avatar asked Jan 10 '20 19:01

Mahesh Madushanka


3 Answers

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"
      }
    }
  ]
}
like image 154
MinhTC Avatar answered Oct 14 '22 12:10

MinhTC


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", 
            ....
            ....
        }
    }
}
like image 34
s33h Avatar answered Oct 14 '22 11:10

s33h


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"
                 }
        }
    ]
}
like image 35
Sambit Avatar answered Oct 14 '22 13:10

Sambit