隨筆 - 45, 文章 - 6, 評(píng)論 - 4, 引用 - 0
          數(shù)據(jù)加載中……

          Java中調(diào)用外部命令

          Java中調(diào)用外部命令
          public class ExecCommond{
          public ExecCommond(){}
          /**
          * 執(zhí)行一條命令
          * @param execStr String 命令字符串
          * @return String 執(zhí)行命令錯(cuò)誤時(shí)的信息。
          */
          public static String exec(String execStr) {
          Runtime runtime = Runtime.getRuntime(); 取得當(dāng)前運(yùn)行期對(duì)象
          String outInfo=""; //執(zhí)行錯(cuò)誤的輸出信息
          try {
          String[] args = new String[] {"sh", "-c", execStr};//執(zhí)行l(wèi)inux下的命令
          //執(zhí)行windows下的命令
          // String[] args = new String[] {"cmd", "-c", execStr};
          Process proc = runtime.exec(args); //啟動(dòng)另一個(gè)進(jìn)程來執(zhí)行命令
          InputStream in = proc.getErrorStream();//得到錯(cuò)誤信息輸出。
          BufferedReader br = new BufferedReader(new InputStreamReader(in));
          String line = "";
          while ( (line = br.readLine())
          != null) {
          outInfo = outInfo + line + "\n";
          System.out.println(outInfo);
          }
          // 檢查命令是否失敗。
           
          try {
          if (proc.waitFor() != 0) {
          System.err.println("exit value = " +
          proc.exitValue());
          }
          }
          catch (InterruptedException e) {
          System.err.print(e);
          e.printStackTrace();
          }
          評(píng)書:
          ps:
           1 import java.io.BufferedReader;
           2 import java.io.IOException;
           3 import java.io.InputStream;
           4 import java.io.InputStreamReader;
           5 import java.io.OutputStream;
           6 
           7 /** 
           8  * 
           9  *  @author  liyang
          10  */
          11 
          12 public class ConsoleStream {
          13 
          14     public static void main(String[] args) throws IOException, InterruptedException {
          15         String info;
          16         System.out.println("Here we go ");
          17         String[] cmd = new String[] {"cmd","/c","javac d://helloworld.java"};
          18         Process p = Runtime.getRuntime().exec(cmd);
          19 
          20 //        OutputStream os  =  p.getOutputStream();
          21         InputStream stdin  =  p.getInputStream(); //
          22         InputStream stderr  =  p.getErrorStream();
          23         
          24         InputStreamReader isr = new InputStreamReader(stdin);
          25         InputStreamReader esr = new InputStreamReader(stderr);
          26         
          27         BufferedReader readerIn = new BufferedReader(isr);
          28         BufferedReader readerErr= new BufferedReader(esr);
          29         
          30         System.out.println("in:");
          31         while ((info = readerIn.readLine()) != null) {
          32             System.out.println(info);
          33         }
          34         
          35         System.out.println("error");
          36         while ((info = readerErr.readLine()) != null) {
          37             System.out.println(info);
          38         }
          39         int retcode;
          40         retcode = p.waitFor();
          41 
          42         System.out.println("process end code: " + retcode);
          43         p.destroy();
          44     }
          45 } 

          posted on 2008-10-27 15:52 liyang 閱讀(296) 評(píng)論(0)  編輯  收藏

          主站蜘蛛池模板: 原阳县| 泽库县| 丽江市| 利川市| 永登县| 贺州市| 方山县| 长治县| 长乐市| 大石桥市| 麻栗坡县| 开鲁县| 大英县| 张掖市| 丰台区| 福州市| 平和县| 景德镇市| 上蔡县| 西畴县| 东源县| 筠连县| 兴宁市| 上栗县| 长白| 南充市| 虎林市| 乐都县| 文昌市| 梨树县| 香港 | 横山县| 科尔| 溆浦县| 徐水县| 仪征市| 玛曲县| 平昌县| 中卫市| 诸城市| 来宾市|