在java中获取环境变量值 [英] Getting environment variable value in java

查看:508
本文介绍了在java中获取环境变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows机器(Windows 7)中设置了环境变量,并尝试从以下java代码中获取变量的值。但是,它返回一个空值。

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.

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") 

后来我在JRE安全策略文件中添加了权限并添加了以下条目,但仍然返回空值。

Later I have added permission in the JRE security policy file and add the following entry but still it returns null value.

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


推荐答案

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

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

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

这篇关于在java中获取环境变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆