參考程式碼如下:
import java.io.*;
public class AppLauncher {
public static void main(String[] args) throws Exception
{
/*
*若需外部傳入可用下列語法,執行時ex:java AppLauncher "cmd.exe /c c:123.bat"
*/
//String cmd = args.length == 0? "java -version" : args[0];
String[] cmd = new String[3];
cmd[0] = "cmd.exe" ;
cmd[1] = "/C" ;
//填入執行程式路徑
cmd[2] = "F:/LMSDB_backup.bat" ;
Process process = Runtime.getRuntime().exec(cmd);
new Thread(new SubTask(process.getInputStream())).start();
new Thread(new SubTask(process.getErrorStream())).start();
process.waitFor();
//多印一空白行
System.out.println();
System.out.println("====================Application terminated=======================");
}
}
class SubTask implements Runnable {
public SubTask(InputStream istream)
{
if (istream == null){
iReader = new BufferedReader(new InputStreamReader(System.in));
else
iReader = new BufferedReader(new InputStreamReader(istream));
}
public void run()
{
try {
String input = iReader.readLine();
while (input != null)
{
//若需執行結果可將input傳出
System.out.println(input);
input = iReader.readLine();
}
}
catch (IOException ioe) {
}
}
private BufferedReader iReader;
}
留言列表