在java中获取环境变量值

时间:2022-03-04 22:46:38

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安全策略文件中添加了权限并添加了以下条目,但仍返回null值。

permission java.lang.RuntimePermission "getenv.PE_CONF_PWD";

1 个解决方案

#1


63  

This code snippet should not return null in order for your problem to be resolved...

此代码段不应返回null以便解决您的问题...

final String value = System.getenv("PE_CONF_PWD");

#1


63  

This code snippet should not return null in order for your problem to be resolved...

此代码段不应返回null以便解决您的问题...

final String value = System.getenv("PE_CONF_PWD");