java – 如何杀死runtime.exec?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 如何杀死runtime.exec?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2959字,纯文字阅读大概需要5分钟。
内容图文
![java – 如何杀死runtime.exec?](/upload/InfoBanner/zyjiaocheng/697/47070fde9d874f338b6adc2f3b06da3a.jpg)
我在我的jframe中使用下面的代码,以便通过java程序执行shell脚本,但是当我实现其他方法来停止运行过程时,此方法被第一个阻止,我无法停止我的程序.所以我要杀死新进程,问题是如何获取进程的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);
}
}
解决方法:
为此,您可以使用java.util.concurrent中的功能
在包含UI的现有类中,您必须添加类似于此的内容:
//class variable to store the future of your task
private Future<?> taskFuture = null;
//to be called from button "Start" action handler
public void actionStart() {
//don't double start, if there is one already running
if(taskFuture == null || taskFuture.isDone()) {
//create the new runnable instance, with the proper commands to execute
MyShellExecutor ex = new MyShellExecutor(new String[] { "sh",testPath + "/install.sh", cmd, "&" });
//we only need one additional Thread now, but this part can be tailored to fit different needs
ExecutorService newThreadExecutor = Executors.newSingleThreadExecutor();
//start the execution of the task, which will start execution of the shell command
taskFuture = newThreadExecutor.submit(ex);
}
}
//to be called from button "Stop" action handler
public void actionStop() {
//if not already done, or cancelled, cancel it
if(taskFuture !=null && !taskFuture.isDone()) {
taskFuture.cancel(true);
}
}
执行该作业的主要组件是Runnable,我将其命名为MyShellExecutor,如下所示:
public class MyShellExecutor implements Runnable {
//stores the command to be executed
private final String[] toExecute;
public MyShellExecutor(String[] toExecute) {
this.toExecute=toExecute;
}
public void run() {
Runtime runtime = Runtime.getRuntime();
Process process = null;
try {
process = runtime.exec(toExecute);
int exitValue = process.waitFor();
System.out.println("exit value: " + exitValue);
BufferedReader buf = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
while ((line = buf.readLine()) != null) {
System.out.println("exec response: " + line);
//do whatever you need to do
}
} catch (InterruptedException e) {
//thread was interrupted.
if(process!=null) { process.destroy(); }
//reset interrupted flag
Thread.currentThread().interrupt();
} catch (Exception e) {
//an other error occurred
if(process!=null) { process.destroy(); }
}
}
}
注意:在运行耗时的操作时,请确保不要在UI线程上执行此操作.这只是阻止用户,并没有提供良好的用户体验.总是做一些可能让用户在另一个线程上等待的东西.
推荐阅读:Java Concurrency In Practice
内容总结
以上是互联网集市为您收集整理的java – 如何杀死runtime.exec?全部内容,希望文章能够帮你解决java – 如何杀死runtime.exec?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。