如果用exec执行的子进程也是你自己写的话
可以在子进程运行完成后system.out.println()输出提示信息
然后在父进程中得到process.getInputStream()
读出字进程的输出
判断后调用p.destroy();
可以在子进程运行完成后system.out.println()输出提示信息
然后在父进程中得到process.getInputStream()
读出字进程的输出
判断后调用p.destroy();
exec执行的是数据库备份语句.
我做了个测试程序,用我说的方法是可行的
我的子进程是一个dialog,包含一个button
button点击时打印over
在父进程中,得到子进程的输出,判断是over则p.destroy();
这样是可以的。不过出现一点小问题
父进程得到流时,只能read到子进程的第一次输出。
用while循环读就可以了
Process p=sys.exec("cmd.exe /c "+strCmd);
//得到命令返回数据
DataInputStream in=new DataInputStream(p.getInputStream());
BufferedReader reader=new BufferedReader(new InputStreamReader(in));String line;
while((line=reader.readLine())!=null){
System.out.println(line);
}
reader.close();
in.close();p.destroy();
strCmd 如果是 dir 则正常
如果是备份语句 exp ....则无输出,进程也结束不了.