wiflish 发表于 2013-2-7 09:59:56

通过java获取系统环境变量

代码如下:
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!----> 1     public static Map getEnv() {
 2         Map map = new HashMap();
 3         String OS = System.getProperty("os.name").toLowerCase();
 4         
 5         Process p = null;
 6         
 7       /**
 8          * 以windows为例.
 9          */
10         if(OS.indexOf("windows") > -1) {
11             try {
12                 p = Runtime.getRuntime().exec("cmd /c set");
13                 BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
14                 
15                 String line;
16                 
17                 while((line = br.readLine()) != null) {
18                     String[] str = line.split("=");
19                     map.put(str[0], str[1]);
20                 }
21             } catch(IOException ioe) {
22                 ioe.printStackTrace();
23             }
24         }
25         return map;
26     }
页: [1]
查看完整版本: 通过java获取系统环境变量