当尝试运行子进程时,它仅打印标准输出然后终止
我正在使用 Apache Commons Exec 并尝试启动在整个应用程序持续时间内工作的子进程。它应该启动进程,接受两个输入命令并停留在后台。现在它只接受一个命令(至少是标准输出显示的)并终止。你能帮我吗?
CommandLine cmdLine = new CommandLine("app.exe");
cmdLine.addArgument("argument");
DefaultExecutor executor = new DefaultExecutor();
OutputStream os = new ByteArrayOutputStream();
InputStream is = new ByteArrayInputStream(("command1;\ncommand2;\n").getBytes());
executor.setStreamHandler(new PumpStreamHandler(os,null,is));
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
executor.execute(cmdLine,resultHandler);
System.out.println(os.toString());
resultHandler.waitFor();
I'm using Apache Commons Exec and trying to start subprocess that would work for the entire duration of application. It should start process, accepttwo input commands and just stay in background. Now its only accepts one command (at least what stdout shows) and terminates. Can you aid me?
CommandLine cmdLine = new CommandLine("app.exe");
cmdLine.addArgument("argument");
DefaultExecutor executor = new DefaultExecutor();
OutputStream os = new ByteArrayOutputStream();
InputStream is = new ByteArrayInputStream(("command1;\ncommand2;\n").getBytes());
executor.setStreamHandler(new PumpStreamHandler(os,null,is));
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
executor.execute(cmdLine,resultHandler);
System.out.println(os.toString());
resultHandler.waitFor();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这两行的顺序错误:
应该是这样的(以允许过程完成其输出):
编辑
仍然不能 100% 确定你在做什么,但我想我错过了“只需留在后台”您原始请求的一部分。实现此目的的一种方法是使用 PipedInputStream &
PipedOutputStream
配对与进程对话。完成后,您可以关闭输出流。如果您想在进程完成之前访问其输出,则可以对方向相反的输出使用类似的技术。我手头没有 Windows 机器,但以下方法对我有用:
I think these two lines are in the wrong order:
Should be like this (to allow the process to complete it's output):
EDIT
Still not 100% sure what you are after, but think I missed the "just stay in background" part of your original request. One way to achieve this would be to use a
PipedInputStream
&PipedOutputStream
pair to talk to the process. When you are done, you can close the output stream. If you wanted access to the output from the process before it finishes, you could use a similar technique for the output with the direction reversed.I don't have a windows machine handy, but the following works for me: