伊峰獨居

            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 伊峰 閱讀(396) 評論(0)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 石门县| 阿坝县| 长沙县| 墨玉县| 阳泉市| 威远县| 澳门| 株洲县| 萍乡市| 湘西| 开封市| 麦盖提县| 定远县| 肇东市| 海丰县| 桃江县| 临沂市| 丰顺县| 义马市| 隆回县| 新乡县| 常德市| 闽侯县| 科技| 云林县| 湟源县| 濮阳市| 阿勒泰市| 岑巩县| 通州市| 岗巴县| 荣成市| 南和县| 三台县| 赤峰市| 于都县| 东乡县| 信阳市| 宜章县| 宜宾县| 天等县|