简体   繁体   English

如何在 VS Code 中设置 JAVA 环境变量

[英]How to set JAVA environment variable in VS Code

I have a spring-boot project and my IDE is VS code.我有一个 spring-boot 项目,我的 IDE 是 VS 代码。 I want to pass an environment variable to my applications.我想将环境变量传递给我的应用程序。 Right now I set it before the Gradle command现在我把它设置在 Gradle 命令之前

export PROJECT_NAME=test

./gradlew bootrun

PROJECT_NAME is my env variable and I access this in application.properties PROJECT_NAME 是我的环境变量,我在 application.properties 中访问它

what is the recommended approach to set environment variables in VS code for java在 java 的 VS 代码中设置环境变量的推荐方法是什么

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:为了在 VSCode 中为 Spring boot 应用程序设置环境变量,推荐的方法是在项目的 .vscode 文件夹中创建一个 launch.json 文件,然后添加“env”部分,如下例所示:

{
  "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.我在尝试使用自定义环境变量运行 JUNIT 测试时遇到了同样的问题。 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.相反,根据本文档,您必须在 setting.json 文件中创建一个 env 对象。 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,如果您使用的是以下版本的 VSCode,您可以看到帮助 -> 关于,

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.只需go到运行菜单,点击打开配置,如下图。

在此处输入图像描述

This will open launch.json , now you can add env details specific to your environment details.这将打开launch.json ,现在您可以添加特定于您的环境详细信息的环境详细信息。 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"
                 }
        }
    ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM