伊峰獨居

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            30 隨筆 :: 30 文章 :: 5 評論 :: 0 Trackbacks

          .bat文件執行的時候,如果有屏幕輸出,就必須要讀取這個輸出,如果不讀取,就會導致這個bat不會被執行下去。所以,只要用個InputStream把輸出讀出來就ok了。

          另外,誰也不能保證Process運行以后系統的PATH到底是什么,所以建議寫上調用的bat的絕對路徑

          import java.io.IOException;
          import java.io.InputStream;

          public class TestProcess {
          ? ? public static void main(String[] args) {
          ? ? ? ? ? String command = "c:\\aaa.bat";
          ? ? ? ? ? try {
          ? ? ? ? ? ? ? ? Process child = Runtime.getRuntime().exec(command);
          ? ? ? ? ? ? ? ? InputStream in = child.getInputStream();
          ? ? ? ? ? ? ? ? int c;
          ? ? ? ? ? ? ? ? while ((c = in.read()) != -1) {
          ? ? ? ? ? ? ? ? ? ? System.out.print(c);//如果你不需要看輸出,這行可以注銷掉
          ? ? ? ? ? ? ? ? }
          ? ? ? ? ? ? ? ? in.close();
          ? ? ? ? ? ? ? ? try {
          ? ? ? ? ? ? ? ? ? ? child.waitFor();
          ? ? ? ? ? ? ? ? } catch (InterruptedException e) {
          ? ? ? ? ? ? ? ? ? ? // TODO Auto-generated catch block
          ? ? ? ? ? ? ? ? ? ? e.printStackTrace();
          ? ? ? ? ? ? ? ? }
          ? ? ? ? ? ? ? ? System.out.println("done");
          ? ? ? ? ? } catch (IOException e) {
          ? ? ? ? ? ? ? ? // TODO Auto-generated catch block
          ? ? ? ? ? ? ? ? e.printStackTrace();
          ? ? ? ? ? }
          ? ? }
          }

          posted on 2006-08-25 13:48 伊峰 閱讀(393) 評論(0)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 若尔盖县| 盱眙县| 雅安市| 万州区| 柳林县| 临高县| 铜梁县| 吴川市| 桐乡市| 宜章县| 鄂伦春自治旗| 井陉县| 霞浦县| 荥阳市| 大竹县| 沿河| 南和县| 镇江市| 沅江市| 渝中区| 铜川市| 申扎县| 陆良县| 喜德县| 丹棱县| 东方市| 新昌县| 伊宁市| 中方县| 文成县| 永靖县| 庆安县| 曲阳县| 浦北县| 富裕县| 雅江县| 彰化县| 宣城市| 腾冲县| 鄂托克前旗| 忻城县|