Java 在“VM 选项”和“程序参数”中使用环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21558855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using environment variables in "VM options" and "Program arguments"
提问by Amelio Vazquez-Reina
In my project configuration in IDEA, I have the following text fields:
在 IDEA 的项目配置中,我有以下文本字段:
I would like to define some environment variables, and refer to them in the fields "VM options"
and "Program arguments"
.
我想定义一些环境变量,并在字段"VM options"
和"Program arguments"
.
I tried with the following definitions for environment variables:
我尝试使用以下环境变量定义:
MY_FOLDER=/some/random/path
MY_ARGUMENT=2
and then in "VM options"
I entered:
然后在"VM options"
我输入:
-Dfoo=$MY_FOLDER
——Dfoo=$MY_FOLDER
and in program arguments I entered
在我输入的程序参数中
$MY_ARGUMENT foo bar
However, the environment variables do not seem to be resolvedprior to calling my class, i.e. if I inspect args[0]
in Java, it holds the string value $MY_ARGUMENT
, not2
.
但是,在调用我的类之前似乎没有解析环境变量,即如果我args[0]
在 Java 中检查,它保存字符串 value $MY_ARGUMENT
, 而不是2
。
Why? and how can I fix this?
为什么?我该如何解决这个问题?
采纳答案by David Miguel
You can access environment variables using the brace-enclosed environment variable syntax. Example:
您可以使用大括号括起来的环境变量语法访问环境变量。例子:
VM options: -Dfoo=${MY_ENV_VAR}
虚拟机选项: -Dfoo=${MY_ENV_VAR}
MY_ENV_VAR
environment variable will be expanded properly.
MY_ENV_VAR
环境变量将被正确扩展。
Update:tested in IntelliJ IDEA 2017.1.2 and still working.
更新:在 IntelliJ IDEA 2017.1.2 中测试并且仍然有效。
回答by Eric Woodruff
Environment variables are not the same thing as program arguments. If you want to pass program arguments then you would call
环境变量与程序参数不同。如果你想传递程序参数,那么你会调用
java MyMainClass /some/random/path 2
Then args[] would contain
然后 args[] 将包含
0: /some/random/path
1: 2
回答by user64204
$ENV_VAR$ will be resolved correctly.
$ENV_VAR$ 将被正确解析。
Idea uses its own notation for environment variables, they should be denoted with two dollar signs on both sides.
Idea 对环境变量使用自己的符号,它们应该在两边用两个美元符号来表示。