在jdk1.5中可以通过System.getenv()获取系统环境变量,但是在jdk1.4.2中该方法不能用,请问还有什么办法获取系统环境变量,向linux中的getenv一样?多谢!

解决方案 »

  1.   

    楼主:
        看看jdk1.5中ProcessEnvironment.java怎么实现的,你就知道怎么在jdk1.4中实现了。
      

  2.   

    Properties p = System.getProperties() ;
            System.getProperties().list(System.out);
      

  3.   

    原来System.getProperties()可以打出系统变量,一下。
      

  4.   

    System.getProperties()试过了,取得不了,获取我自定义的环境变量总返回null.
      

  5.   

    System.out.println(System.getProperty("java.class.path"));
      

  6.   

    我自定义的环境变量PP_ROOT怎么获取呢,java.class.path里也没有PP_ROOT这个变量阿!?
      

  7.   

    public static String getEnv(String name)
    {
    StringBuffer sb = new StringBuffer();
    try
    {
    Process p = Runtime.getRuntime().exec("cmd /c echo "+ '%' + name + '%');BufferedReader br = new BufferedReader (new InputStreamReader(p.getInputStream()));
    String strLine = null;
    while ((strLine = br.readLine ())!= null)
    {
    sb.append(strLine);
    }
    }
    catch(Exception e) {}return sb.toString();
    }
      

  8.   

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.HashMap;
    import java.util.Map;public class b {
    public static Map getEnv() {
             Map map = new HashMap();                      
              Process p = null;  
             
                 try {
                     p = Runtime.getRuntime().exec("cmd /c set");
                    BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
                     
                     String line;
                     
                     while((line = br.readLine()) != null) {
                         String[] str = line.split("=");
                         map.put(str[0], str[1]);
                     }
                 } catch(IOException e) {
                     e.printStackTrace();
                 }          
             return map;
         }
    public static void main(String[] args){
    Map map = getEnv();
    System.out.print(map.get("CLASSPATH"));

    }}