在java中获取环境变量值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18127534/
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
Getting environment variable value in java
提问by Shamim Ahmmed
An environment variable has been set in windows machine (windows 7) and trying to get the value of the variable from the following java code. However, it returns a null value.
已在 windows 机器(windows 7)中设置了一个环境变量,并尝试从以下 java 代码中获取该变量的值。但是,它返回一个空值。
final String value = System.getenv("PE_CONF_PWD");
System.out.println(value);
//print null
Now if I execute the following code, it throws a security exception that means that the user does not have the variable.
现在,如果我执行以下代码,它会抛出一个安全异常,这意味着用户没有该变量。
SecurityManager m = new SecurityManager();
m.checkPropertyAccess("PE_CONF_PWD");
// java.security.AccessControlException: access denied ("java.util.PropertyPermission" "PE_CONF_PWD" "read")
Later I have added permission in the JRE security policy file and add the following entry but still it returns null value.
后来我在 JRE 安全策略文件中添加了权限并添加了以下条目,但它仍然返回空值。
permission java.lang.RuntimePermission "getenv.PE_CONF_PWD";
采纳答案by Ankit
This code snippet should not return null in order for your problem to be resolved...
为了解决您的问题,此代码片段不应返回 null...
final String value = System.getenv("PE_CONF_PWD");