加载进程但不启动它

加载进程但不启动它

本文介绍了ProcessBulder 加载进程但不启动它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ProcessBuilder 从 Java 应用程序(主机)启动一个新进程(子进程).像这样:

I use ProcessBuilder to start a new process (child) form a java application (host). Something like this:

ProcessBuilder processBuilder = createProcess(commandLine);
processBuilder.directory(new File(baseDir));
processBuilder.redirectErrorStream(true);
Process process = null;
try {
    process = processBuilder.start();
} catch (Exception e) {
    e.printStackTrace();
}

我确实在系统监视器中看到子进程已启动,但除非我停止主机应用程序,否则它无法运行.更具体地说,子进程是一个服务器,在使用 ProcessBuilder 启动它后,如果主机应用程序仍在运行,它不会响应请求.此外,服务器正在使用的端口仍然可用.如果我停止主机应用程序,服务器会立即开始工作.有什么我错过的或者 ProcessBuilder 应该如何工作?提前谢谢了.

I do see in the system monitor that the child process has been started but it's not functioning unless I stop the host application. More specifically the child proecess is a server and after starting it with a ProcessBuilder it's not responding to the requests if the host application still is running. Moreover, the port that server is using still is available. The server starts working immediately if I stop the host application. Is there anything that I missed or that's how ProcessBuilder suppose to work?Many thanks in advance.

推荐答案

在大多数情况下,在进程的标准输出缓冲区被清空之前,它不会终止.可能是您的进程已填满此缓冲区并已停止(出于某种原因)

Under most circumstances, until a process's standard out buffer is emptied, it will not terminate. It might be that your process has filled this buffer and has stopped (for some reason)

尝试使用标准输出的流程(通过 Process#getInputStream),看看是否有所不同.

Try consuming the processes standard out (via Process#getInputStream) and see if that makes a difference.

也可能是进程正在等待用户的输入.

It could also be that the process is waiting for input for the user.

看看我没有得到任何输出,可能机器挂了代码 举个例子

这篇关于ProcessBulder 加载进程但不启动它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 04:02