一. System.getProperties() 使用
Properties props = System.getProperties();
System.out.println("props共有" + props.size() + "个属性");
int index = 0;
for(Iterator<Entry<Object, Object>> it = props.entrySet().iterator();it.hasNext();) {
Entry<Object, Object> entry = it.next();
System.out.println((++index) + ". " + entry);
}
二. System.getenv() 使用
index = 0;
Map m = System.getenv();
for(Iterator it = m.entrySet().iterator();it.hasNext();) {
System.out.println((++index) + ". " + it.next());
}
三. 区别
1. System.getProperties() 是获取java系统的属性,主要是java虚拟机运行时的一些环境变量参数,可以通过java -DparamName=value来设置,
在eclipse中可以这样来设置
![](https://img.haomeiwen.com/i27344730/20e4e88fa2a489e0.png)
2. System.getenv() 是用来获取操作系统的环境变量,在eclipse中作如下配置
![](https://img.haomeiwen.com/i27344730/43e7a609cef47090.png)
![](https://img.haomeiwen.com/i27344730/9ad954653dc74cb3.png)
3. java虚拟机的参数系统可以在代码中维护,而操作系统的环境变量只能读取
手动设置虚拟机变量:System.setProperty("sun.cpu.isalist", System.getProperty("sun.cpu.isalist") + "abc");
网友评论