【发布时间】:2011-02-28 06:39:46
【问题描述】:
我需要在 java 中执行一个具有特定超时的外部批处理文件。这意味着如果批处理执行时间超过指定的超时时间,我需要取消执行。
这是我编写的示例代码:
public static void main(String[] args) throws IOException, InterruptedException {
ProcessBuilder p = new ProcessBuilder("c:\\wait.bat", "25"); // batch file execution will take 25 seconds.
final long l = System.currentTimeMillis();
System.out.println("starting..." + (System.currentTimeMillis() - l));
final Process command = p.start();
System.out.println("started..." + (System.currentTimeMillis() - l));
Timer t = new Timer();
t.schedule(new TimerTask() {
@Override
public void run() {
command.destroy();
}
}, 5000); // it will kill the process after 5 seconds (if it's not finished yet).
int i = command.waitFor();
t.cancel();
System.out.println("done..." + (System.currentTimeMillis() - l));
System.out.println("result : " + i);
System.out.println("Really Done..." + (System.currentTimeMillis() - l));
}
批处理文件“wait.bat”是这样的:
@echo off
echo starting the process...
@ping 127.0.0.1 -n 2 -w 1000 > nul
@ping 127.0.0.1 -n %1% -w 1000> nul
echo process finished succesfully
@echo on
正如您在代码中看到的,批处理文件需要 25 秒才能完成(main 方法中的第一行),Timer 将在 5 秒后销毁命令。
这是我的代码的输出:
starting...0
started...0
done...5000
result : 1
Really Done...5000
BUILD SUCCESSFUL (total time: 25 seconds)
正如您在输出中看到的,最后一行(“Really Done...”)在第 5 秒内执行,但应用程序在 25 秒后完成。
我的问题是:即使我在计时器中调用了 destroy 方法,为什么 jvm 仍在等待进程完成?
【问题讨论】:
-
这个“BUILD SUCCESSFUL”在哪里出现
-
@Suraj :它的 netbeans 默认消息将在应用程序终止后显示。
-
我知道这是一个相当古老的对话,但 process.destroy() 适用于 JDK 1.7.0_51。