posts - 241,  comments - 116,  trackbacks - 0

          用java調(diào)用shell,使用

          Process p=Runtime.getRuntime().exec(String[] cmd);

          Runtime.exec方法將產(chǎn)生一個本地的進程,并返回一個Process子類的實例,該實例可用于控制進程或取得進程的相關(guān)信息。
          由于調(diào)用Runtime.exec方法所創(chuàng)建的子進程沒有自己的終端或控制臺,因此該子進程的標(biāo)準(zhǔn)IO(如stdin,stdou,stderr)都通過
              p.getOutputStream(),
              p.getInputStream(),
              p.getErrorStream()
          方法重定向給它的父進程了.用戶需要用這些stream來向 子進程輸入數(shù)據(jù)或獲取子進程的輸出。
              
              例如:Runtime.getRuntime().exec("ls")


          另外需要關(guān)心的是Runtime.getRuntime().exec()中產(chǎn)生停滯(阻塞,blocking)的問題?


              這個是因為Runtime.getRuntime().exec()要自己去處理stdout和stderr的輸出,
              就是說,執(zhí)行的結(jié)果不知道是現(xiàn)有錯誤輸出(stderr),還是現(xiàn)有標(biāo)準(zhǔn)輸出(stdout)。
              你無法判斷到底那個先輸出,所以可能無法讀取輸出,而一直阻塞。
              例如:你先處理標(biāo)準(zhǔn)輸出(stdout),但是處理的結(jié)果是先有錯誤輸出(stderr),
              一直在等錯誤輸出(stderr)被取走了,才到標(biāo)準(zhǔn)輸出(stdout),這樣就產(chǎn)生了阻塞。

          解決辦法:


              用兩個線程將標(biāo)準(zhǔn)輸出(stdout)和錯誤輸出(stderr)。

          import java.util.*;
          import java.io.*;
          class StreamGobbler extends Thread
          {
              InputStream is;
              String type;
              
              StreamGobbler(InputStream is, String type)
              {
                  this.is = is;
                  this.type = type;
              }
              
              public void run()
              {
                  try
                  {
                      InputStreamReader isr = new InputStreamReader(is);
                      BufferedReader br = new BufferedReader(isr);
                      String line=null;
                      while ( (line = br.readLine()) != null)
                          System.out.println(type + ">" + line);    
                      } catch (IOException ioe)
                        {
                          ioe.printStackTrace();  
                        }
              }
          }
          public class ExecRunner
          {
              public static void main(String args[])
              {
                  if (args.length < 1)
                  {
                      System.out.println("USAGE: java GoodWindowsExec <cmd>");
                      System.exit(1);
                  }
                  
                  try
                  {            
                      String osName = System.getProperty("os.name" );
                      String[] cmd = new String[3];
                      if( osName.equals( "Windows NT" ) )
                      {
                          cmd[0] = "cmd.exe" ;
                          cmd[1] = "/C" ;
                          cmd[2] = args[0];
                      }
                      else if( osName.equals( "Windows 95" ) )
                      {
                          cmd[0] = "command.com" ;
                          cmd[1] = "/C" ;
                          cmd[2] = args[0];
                      } else {
                          StringTokenizer st = new StringTokenizer(command, " ");
                          cmd = new String[st.countTokens()];
                          int token = 0;
                          while (st.hasMoreTokens()) {
                              String tokenString = st.nextToken();
                              // System.out.println(tokenString);
                              cmd[token++] = tokenString;
                          }
                      }
                      
                      Runtime rt = Runtime.getRuntime();
                      System.out.println("Execing " + cmd[0] + " " + cmd[1]
                                         + " " + cmd[2]);
                      Process proc = rt.exec(cmd);
                      // any error message?
                      StreamGobbler errorGobbler = new
                          StreamGobbler(proc.getErrorStream(), "ERROR");            
                      
                      // any output?
                      StreamGobbler outputGobbler = new
                          StreamGobbler(proc.getInputStream(), "OUTPUT");
                          
                      // kick them off
                      errorGobbler.start();
                      outputGobbler.start();淘寶女裝夏裝新款
                                              
                      // any error???
                      int exitVal = proc.waitFor();
                      System.out.println("ExitValue: " + exitVal);        
                  } catch (Throwable t)
                    {
                      t.printStackTrace();
                    }
              }
          }

          posted on 2011-05-16 16:04 墻頭草 閱讀(2642) 評論(0)  編輯  收藏

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


          網(wǎng)站導(dǎo)航:
           
          人人游戲網(wǎng) 軟件開發(fā)網(wǎng) 貨運專家
          主站蜘蛛池模板: 台安县| 柯坪县| 永顺县| 旬邑县| 建德市| 桐梓县| 博罗县| 巴彦淖尔市| 新丰县| 沛县| 永丰县| 临颍县| 新宁县| 大田县| 江都市| 荃湾区| 佛冈县| 景宁| 崇州市| 南陵县| 石渠县| 如皋市| 沧州市| 寿光市| 麻江县| 辉南县| 桂东县| 荣昌县| 奉贤区| 兴城市| 浠水县| 佛山市| 石棉县| 安龙县| 榕江县| 马山县| 孝感市| 泗阳县| 临漳县| 玛曲县| 文山县|