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
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"
}
}
]
}
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",
....
....
}
}
}
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.
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"
}
}
]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With