p.waitFor() 不等待进程完成(Java) [英] p.waitFor() does not wait for process to complete (Java)

查看:71
本文介绍了p.waitFor() 不等待进程完成(Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是该线程的衍生:在处理时杀死 JFrame/Java结束

我知道这个问题在 SO 上被问过多次,但似乎没有一个答案能解决我的问题.我遇到的问题是:我有一个调用批处理文件的 java 程序.我需要在 Java 程序继续之前完成批处理文件.为此,我使用 waitFor() 方法,该方法应该等到进程完成.然而,事实并非如此.我找到了这个线程,它表明进程 waitFor() 实际上只是在等待 cmd.exe,它在实际进程执行之前返回.Runtime.exec().waitFor() 不会等到过程完成该线程建议使用/wait 命令,但是当我这样做时没有区别.我的代码非常深入,包含几个相互依赖的类,但这是一个尝试的 SSCCE:

I know this question has been asked various times here on SO but none of the answers seem to solve my problem. The issue I am having is this: I have a java program that calls a batch file. I need the batch file to finish before the Java program continues. For this I use the waitFor() method, which should wait until the process finishes. However, it doesn't. I found this thread, which suggests that the process waitFor() is actually waiting for is simply cmd.exe, which returns before the actual process does. Runtime.exec().waitFor() doesn't wait until process is done That thread recommends using the /wait command, but when I do it makes no difference. My code is very in-depth with several classes that all rely on one another, but here is an attempted SSCCE:

public class BatchFileRun(){
    public static void main(String[] args){
        ArrayList<String> list = new ArrayList<String>();
        list.add("cmd.exe");
        list.add("/C");
        list.add("Start");
        list.add("/wait");
        list.add(("C:\\.....\\scripts\\myBatchFile.bat"));
        list.add("batchVariable");
        String[] commands = new String[list.size()];
        commands = list.toArray(commands);
        Process p = Runtime.getRuntime().exec(commands);
        p.waitFor();
        closeWindow();
    }
    void closeWindow(){
        WindowEvent close = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
        Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(close);
        System.exit(0);
    }
}

批处理文件只是对作为参数传递给它的变量(batchVariable)进行操作.所有先前的命令都应该用于简单地运行批处理文件.问题是在批处理文件中的进程完成之前调用了 closeWindow() 方法.因此,出现在 Windows 任务管理器中的 Java 进程仍然在运行,即使它应该在调用 closeWindow() 时关闭(如果我正确使用它).当从批处理文件运行的进程完成时,我真的需要关闭该进程.我尝试了不同的运行批处理文件的方法(例如,使用 ProcessBuilder),但无论我尝试什么方法,我都遇到了同样的问题.

The batch file simply operates on the variable passed into it as a parameter (batchVariable). All prior commands should be used to simply run the batch file. The problem is that the closeWindow() method is called before the process in the batch file is finished. Therefore, the Java process that appears in the Windows task manager remains running, even though it should close when closeWindow() is called (if I am using it correctly). I really need said process to close when the process running from the batch file is complete. I have tried different ways of running the batch file (using ProcessBuilder, for example), but no matter what I try I have the same problem.

推荐答案

您可以修改批处理文件,使其仅在它启动的进程完成时才完成.

You can modify the batch file so it only finishes when the process it launches finishes.

这篇关于p.waitFor() 不等待进程完成(Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆