当尝试运行子进程时,它仅打印标准输出然后终止

发布于 2024-11-27 15:46:09 字数 642 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没企图 2024-12-04 15:46:09

我认为这两行的顺序错误:

    System.out.println(os.toString());
    resultHandler.waitFor();

应该是这样的(以允许过程完成其输出):

    resultHandler.waitFor();
    System.out.println(os.toString());

编辑

仍然不能 100% 确定你在做什么,但我想我错过了“只需留在后台”您原始请求的一部分。实现此目的的一种方法是使用 PipedInputStream & PipedOutputStream 配对与进程对话。完成后,您可以关闭输出流。如果您想在进程完成之前访问其输出,则可以对方向相反的输出使用类似的技术。

我手头没有 Windows 机器,但以下方法对我有用:

public static void main(String[] args) {
    try {
        CommandLine cmdLine = new CommandLine("/bin/bash");
        DefaultExecutor executor = new DefaultExecutor();
        OutputStream os = new ByteArrayOutputStream();
        PipedOutputStream pos = new PipedOutputStream();
        PipedInputStream  pis = new PipedInputStream(pos);
        executor.setStreamHandler(new PumpStreamHandler(os, null, pis));
        DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
        executor.execute(cmdLine, resultHandler);

        PrintWriter pw = new PrintWriter(pos);
        pw.println("ls -l /usr");
        pw.println("pwd");
        pw.close();
        resultHandler.waitFor();
        System.out.println(os.toString());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

I think these two lines are in the wrong order:

    System.out.println(os.toString());
    resultHandler.waitFor();

Should be like this (to allow the process to complete it's output):

    resultHandler.waitFor();
    System.out.println(os.toString());

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:

public static void main(String[] args) {
    try {
        CommandLine cmdLine = new CommandLine("/bin/bash");
        DefaultExecutor executor = new DefaultExecutor();
        OutputStream os = new ByteArrayOutputStream();
        PipedOutputStream pos = new PipedOutputStream();
        PipedInputStream  pis = new PipedInputStream(pos);
        executor.setStreamHandler(new PumpStreamHandler(os, null, pis));
        DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
        executor.execute(cmdLine, resultHandler);

        PrintWriter pw = new PrintWriter(pos);
        pw.println("ls -l /usr");
        pw.println("pwd");
        pw.close();
        resultHandler.waitFor();
        System.out.println(os.toString());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文