返回值,我有这样的系统变量为什么System.getProperty不会从系统变量在Windows
验证码:
System.out.println(System.getProperty("area"));
返回null。
我可以通过System.getProperty方法从java代码访问系统变量吗?
P.S
返回值,我有这样的系统变量为什么System.getProperty不会从系统变量在Windows
验证码:
System.out.println(System.getProperty("area"));
返回null。
我可以通过System.getProperty方法从java代码访问系统变量吗?
P.S
这不是一个系统变量而是环境变量。您可以通过使用System.getenv()
来访问这些变量。 System.getProperty()
用于返回启动程序时设置的任何变量。例如:
# java -Dmyproperty=myvalue MyProgram
要列出所有Java System Properties
使用,然后检查,如果你有一个area
属性:
Properties props = System.getProperties();
props.list(System.out);
看到这个example
System#getProperty
让你的系统性能。
您可以访问环境变量地图通过System#getenv
System.out.println(System.getenv("area"));
应该让你的结果。
编辑:
尝试运行下面的代码,如果你需要的变量出现在控制台或行为是其他定义的变量incosistent检查:
public static void main(String[] args) {
Set<String> envSet = System.getenv().keySet();
for (String env : envSet) {
System.out.println("Env Variable : " + env + " has value : "
+ System.getenv(env));
}
}
* System.out.println(System.getenv(“area”)); *返回null – gstackoverflow
@gstackoverflow:打开* new *命令提示符并键入'set area',并告诉我们输出。 – Manish
以cmd输出:* area = 1 * – gstackoverflow
的System.out.println( System.getenv( “区域”));返回null – gstackoverflow
请注意,在java 8中,你应该写'java -Dmyproperty = myvalue -jar myProgram.jar'而不是'java -jar myProgram.jar -Dyproperty = myvalue',或者'myproperty'将为'null' – Searene