robin35java 发表于 2013-2-7 09:59:32

Java获得系统环境变

http://35java.com/zhibo/forum.php?mod=viewthread&tid=356&extra=page%3D4
package org.loon.test;

import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;

/** *//**
* <p>
* Title: LoonFramework
* </p>
* <p>
* Description:
* </p>
* <p>
* Copyright: Copyright (c) 2007
* </p>
* <p>
* Company: LoonFramework
* </p>
*
* @author chenpeng
* @email:ceponline@yahoo.com.cn
* @version 0.1
*/
public
class SystemInfo ...{

    public
final
static String envStr[] =
...{ "unknown OS type",
            "unknown OS version", "unknown OS architecture",
            "unknown JRE version", "unknown JRE vendor" };

    /** *//**
   * 遍历所有设置
   * @param <K>
   * @param <V>
   */
    @SuppressWarnings("unchecked")
    public
static
<K, V>
void systemProperties() ...{
      Properties p = System.getProperties();
      TreeMap<K, V> map =
new TreeMap<K, V>();
      map.putAll((Map<?
extends K, ?
extends V>) p);
      Iterator itr = map.keySet().iterator();
      while (itr.hasNext()) ...{
            String key = (String) itr.next();
            String value = (String) map.get(key);
            System.out.println(key +
"="
+ value);
      }
    }

    /** *//**
   * sun制造标记
   * @param type
   * @return
   */
    public
boolean isSun(String type) ...{
      return type.indexOf("Sun") !=
-1;
    }

    /** *//**
   * applet制造标记
   * @param type
   * @return
   */
    public
boolean isApple(String type) ...{
      return type.indexOf("Apple") !=
-1;
    }

    /** *//**
   * hp制造标记
   * @param type
   * @return
   */
    public
static
boolean isHPUX(String type) ...{
      return type.indexOf("Hewlett-Packard Company") !=
-1;
    }

    /** *//**
   * ibm制造标记
   * @param type
   * @return
   */
    public
static
boolean isIBM(String type) ...{
      return type.indexOf("IBM") !=
-1;
    }

    /** *//**
   * Blackdown制造标记
   * @param type
   * @return
   */
    public
static
boolean isBlackdown(String type) ...{
      return type.indexOf("Blackdown") !=
-1;
    }

    /** *//**
   * bea制造标记
   * @param type
   * @return
   */
    public
static
boolean isBEAWithUnsafeSupport(String type) ...{
      if (type.indexOf("BEA") !=
-1) ...{
            String vmVersion = System.getProperty("java.vm.version");
            if (vmVersion.startsWith("R")) ...{
                return
true;
            }
            String vmInfo = System.getProperty("java.vm.info");
            if (vmInfo !=
null) ...{
                return (vmInfo.startsWith("R25.1") || vmInfo
                        .startsWith("R25.2"));
            }
      }
      return
false;
    }

   void getEnv() ...{
      // os.name 主机操作系统的名称
      // os.version 主机操作系统的版本
      // os.arch 主机操作系统的体系结构
      // java.version java版本
      // java.vendor java厂商
      String as[] =
...{ "os.name", "os.version", "os.arch", "java.version",
                "java.vendor" };
      for (int i =
0; i < as.length; i++) ...{
            try
...{
                envStr = System.getProperty(as);
                System.out.println((as +
"="
+ ((i==4)?envStr+"
"+isSun(envStr):envStr)).intern());
            }
catch (Exception ex) ...{
            }
      }

    }

    public
static
void main(String[] args) ...{
      new SystemInfo().getEnv();
      System.out.println(" 全部设置 ");
      //显示所有设置
      systemProperties();
    }

}
页: [1]
查看完整版本: Java获得系统环境变