【发布时间】:2013-02-26 17:47:49
【问题描述】:
我在我的jframe中使用下面的代码通过java程序执行shell脚本,但是当我实现其他方法来停止运行进程时,这个方法首先被阻止,我无法停止我的程序.所以我msut杀死新进程,问题是如何获取进程的pid以及如何将其置于后台,以便杀死它。
public static void execShellCmd(String cmd) {
try {
testPath = getPath();
System.out.println(cmd);
Runtime runtime = Runtime.getRuntime();
process = runtime.exec(new String[] { "sh",
testPath + "/install.sh", cmd, "&" });
int exitValue = process.waitFor();
value = exitValue;
System.out.println("exit value: " + exitValue);
BufferedReader buf = new BufferedReader(new InputStreamReader(
process.getInputStream()));
String line = "";
BufferedWriter bufferedWriter = null;
while ((line = buf.readLine()) != null) {
System.out.println("exec response: " + line);
log = line;
writeToFile(line);
}
} catch (Exception e) {
System.out.println(e);
}
}
【问题讨论】:
-
这可能会有所帮助:stackoverflow.com/q/5284139/837154
-
谢谢。我认为这是一些问题。但他们没有提出解决方案