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

          導航

          常用鏈接

          留言簿(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
           
          Copyright © lucia Powered by: 博客園 模板提供:滬江博客
          主站蜘蛛池模板: 红原县| 延津县| 成安县| 新龙县| 宁乡县| 定安县| 晋城| 怀仁县| 隆回县| 灵丘县| 宁化县| 崇阳县| 建水县| 岳普湖县| 合肥市| 泰顺县| 稷山县| 文化| 清徐县| 台中市| 鹤庆县| 奉化市| 库车县| 西盟| 遂宁市| 十堰市| 河北省| 绥阳县| 兰考县| 湘西| 固始县| 五寨县| 泉州市| 兰州市| 旌德县| 卫辉市| 罗山县| 东源县| 吴堡县| 阿瓦提县| 集贤县|