學(xué)無止境  
          日歷
          <2005年9月>
          28293031123
          45678910
          11121314151617
          18192021222324
          2526272829301
          2345678
          統(tǒng)計
          • 隨筆 - 9
          • 文章 - 0
          • 評論 - 2
          • 引用 - 0

          導(dǎo)航

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

           
          In JDK 1.4 System.getenv() is deprecated. (In JDK 1.5 it is un-deprected again.) The getProperty method is now the correspoding method to get the variables. For example: System.getProperty("java.class.path",""). But it doesn't work for user defined environment variable. This problem can be solved by using the following code:
          import java.io.BufferedReader;
          import java.io.InputStreamReader;

          /**
          * Environment class simulates the System.getenv() method which is deprecated
          * on java 1.4.2
          *
          * @author v-josp
          *
          */
          public class TestRoot
          {
          //result of all enviornment variables
          private static BufferedReader commandResult;

          static
          {
          String cmd = null;
          String os = null;

          //getting the OS name
          os = System.getProperty("os.name").toLowerCase();

          // according to OS set the command to execute
          if(os.startsWith("windows"))
          {
          cmd = "cmd /c SET";
          }
          else
          {
          cmd="env";
          }

          try
          {
          //execute the command and get the result in the form of InputStream
          Process p = Runtime.getRuntime().exec(cmd);

          //parse the InputStream data
          InputStreamReader isr = new InputStreamReader(p.getInputStream());
          commandResult= new BufferedReader(isr);
          }
          catch (Exception e)
          {
          System.out.println("OSEnvironment.class error: " + cmd + ":" + e);
          }
          }

          /**
          * This method is used to get the path of the given enviornment variable. This
          * method tries to simulates the System.getenv() which is deprecated on java 1.4.2
          *
          * @param String - name of the environment variable
          * @param String - default value
          * @return
          */
          public static String getenv(String envName,String defaultValue)
          {
          String line = null;
          try
          {
          while ((line = commandResult.readLine()) != null)
          {
          if(line.indexOf(envName)>-1)
          {
          return line.substring(line.indexOf(envName)+envName.length()+1);
          }
          }
          return defaultValue;
          }
          catch (Exception e)
          {
          return defaultValue;
          }
          }

          public static void main(String args[])
          {
          System.out.println(TestRoot.getenv("CLASSPATH",""));
          }
          }

          Output
          _____
          F:\software\javaws-1_2-dev\jnlp.jar;
          posted on 2005-09-14 02:36 lucia 閱讀(669) 評論(0)  編輯  收藏 所屬分類: JAVA

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
           
          Copyright © lucia Powered by: 博客園 模板提供:滬江博客
          主站蜘蛛池模板: 民勤县| 兴城市| 屏东县| 逊克县| 湘乡市| 巴青县| 靖宇县| 平顺县| 五原县| 固阳县| 阿拉善右旗| 廊坊市| 昌平区| 栾川县| 调兵山市| 西乡县| 铁力市| 金塔县| 金寨县| 尼木县| 六安市| 大竹县| 商都县| 绍兴市| 临高县| 盐池县| 吉木萨尔县| 清镇市| 乌兰浩特市| 类乌齐县| 通河县| 余江县| 五莲县| 儋州市| 莱西市| 台州市| 酒泉市| 遂溪县| 瑞安市| 五峰| 莲花县|