从 Java 执行的 Windows 进程未终止

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/685641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:12:52  来源:igfitidea点击:

Windows process exec'ed from Java not terminating

javawindows

提问by David Resnick

I'm creating a process on Windows from Java. My problem is that this process doesn't terminate. Here's a sample program:

我正在从 Java 在 Windows 上创建一个进程。我的问题是这个过程不会终止。这是一个示例程序:

import java.io.IOException;

public class Test {

/**
 * @param args
 * @throws IOException
 * @throws InterruptedException
 */
public static void main(String[] args) throws IOException,
        InterruptedException {
    Process process = Runtime.getRuntime().exec("cmd /c dir");
    process.waitFor();
    }
}

For reasons beyond my understanding, this program never completes. This is true if "cmd /c dir" is replaced with ipconfig as well as other things.

由于我无法理解的原因,这个程序永远不会完成。如果将“cmd /c dir”替换为 ipconfig 以及其他内容,则情况确实如此。

I can see using ProcessExplorer that java creates the cmd process. This sample is obviously a simplification; in my original program I found that if I call process.destroy() after a while and check the cmd process output, the command is executed successfully.

我可以看到使用 ProcessExplorer,java 创建了 cmd 进程。这个示例显然是一个简化;在我原来的程序中,我发现如果我在一段时间后调用 process.destroy() 并检查 cmd 进程输出,则该命令执行成功。

I've tried this with various releases of Java 1.5 and 1.6. My OS is Windows XP Pro, SP 2.

我已经在 J​​ava 1.5 和 1.6 的各种版本中尝试过这个。我的操作系统是 Windows XP Pro,SP 2。

回答by Eric Petroelje

Likely that you just need to read the stdout and stderr of the process, or it will hang since its output buffer is full. This is easiest if you redirect stderr to stdout, just to be safe:

可能您只需要读取进程的 stdout 和 stderr,否则它会因为其输出缓冲区已满而挂起。如果您将 stderr 重定向到 stdout,这是最简单的,只是为了安全:

public static void main(String[] args) throws IOException,
                InterruptedException {
        String[] cmd = new String[] { "cmd.exe", "/C", "dir", "2>&1" };
        Process process = Runtime.getRuntime().exec(cmd);
        InputStream stdout = process.getInputStream();
        while( stdout.read() >= 0 ) { ; }
        process.waitFor();
    }
}

回答by kgiannakakis

See this linkfor an explanation.

请参阅此链接以获取解释。

You need to read the input stream. Also the java process doesn't work like a dos shell. You need to pass the arguments yourself:

您需要读取输入流。此外,java 进程不像 dos shell 那样工作。您需要自己传递参数:

String[] cmd = new String[3];
cmd[0] = "cmd.exe" ;
cmd[1] = "/C" ;
cmd[2] = "dir";
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd);

回答by Adam Lerman

You should try using a ProcessBuilderas shown below. Not sure what's different but this solved a LOT of problems for us.

您应该尝试使用ProcessBuilder如下所示的a 。不确定有什么不同,但这为我们解决了很多问题。

ProcessBuilder pb = new ProcessBuilder(CMD, ARG1, ARG2);  
Process p = pb.start();