Java調(diào)用可執(zhí)行文件和批處理命令
|
轉(zhuǎn)帖 |
|
在java調(diào)用exe,com可執(zhí)行文件和bat,cmd批處理文件 一。運行exe文件
???1.?Java?JDK里已經(jīng)提供了調(diào)用的方法,不在累贅,代碼如下。
????try?{ ????????String?command?=?"notepad"; ????????Process?child?=?Runtime.getRuntime().exec(command); ????}?catch?(IOException?e)?{ ????}
二。運行bat(批處理)文件
???1.
import?java.io.*;
public?class?Test { ????public?static?void?main(String[]?args) ????{ ????????System.out.println("args?:?"?+?java.util.Arrays.asList(args));
????????try ????????{ ????????????String?command?=?args.length?==?0???"notepad"?:?args[0]; ????????????Process?child?=?Runtime.getRuntime().exec(command);
????????????String?line?=?null; ????????????BufferedReader?reader?=?new?BufferedReader(new?InputStreamReader(child.getInputStream())); ????????????while((line?=?reader.readLine())?!=?null) ????????????{ ????????????????System.out.println(line); ????????????} ????????} ????????catch?(Exception?ex) ????????{ ????????????ex.printStackTrace(); ????????} ????}
}
???2.?雖然網(wǎng)上有人說找不到直接執(zhí)行bat文件的方法,但我使用這種方法似乎也可以達到執(zhí)行bat文件的效果,原本的希望是看看能從Process中讀到什么信息,結(jié)果直接把bat文件中的內(nèi)容按行打印并執(zhí)行了。 ???3.?但因為InputStream一直沒有關(guān)閉,這個循環(huán)變成了一個死循環(huán),不知道如何判斷批處理文件何時執(zhí)行完畢。
|
|
posted on 2006-04-14 11:05
MEYE 閱讀(542)
評論(0) 編輯 收藏 所屬分類:
JAVA