Trouble providing multiple input to a Command using Apache Commons Exec and extracting output

前端 未结 3 2147
孤街浪徒
孤街浪徒 2021-01-02 13:05

I am writing a Java application that needs to use an external command line application using the Apache Commons Exec library. The application I need to run has a fairly lon

相关标签:
3条回答
  • 2021-01-02 13:28

    To be able to write more than one command in the STDIN of the process, I have create a new

    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    import java.util.Map;
    
    import org.apache.commons.exec.CommandLine;
    import org.apache.commons.exec.DefaultExecutor;
    import org.apache.commons.lang3.CharEncoding;
    
    public class ProcessExecutor extends DefaultExecutor {
    
        private BufferedWriter processStdinput;
    
        @Override
        protected Process launch(CommandLine command, Map env, File dir) throws IOException {
            Process process = super.launch(command, env, dir);
            processStdinput = new BufferedWriter(new OutputStreamWriter(process.getOutputStream(), CharEncoding.UTF_8));
            return process;
        }
    
        /**
         * Write a line in the stdin of the process.
         * 
         * @param line
         *            does not need to contain the carriage return character.
         * @throws IOException
         *             in case of error when writing.
         * @throws IllegalStateException
         *             if the process was not launched.
         */
        public void writeLine(String line) throws IOException {
            if (processStdinput != null) {
                processStdinput.write(line);
                processStdinput.newLine();
                processStdinput.flush();
            } else {
                throw new IllegalStateException();
            }
        }
    
    }
    

    To use this new Executor, I keep the piped stream within the PumpStreamHandler to avoid that the STDIN to be close by the PumpStreamHandler.

    ProcessExecutor executor = new ProcessExecutor();
    executor.setExitValue(0);
    executor.setWorkingDirectory(workingDirectory);
    executor.setWatchdog(new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT));
    executor.setStreamHandler(new PumpStreamHandler(outHanlder, outHanlder, new PipedInputStream(new PipedOutputStream())));
    executor.execute(commandLine, this);
    

    You can use the executor writeLine() method or create your own.

    0 讨论(0)
  • 2021-01-02 13:29

    I ended up figuring out a way to make this work. By looking inside the code of the Commons Exec library, I noticed the StreamPumpers used by the PumpStreamHandler did not flush each time they had some new data incoming. This is why the code worked when I executed it just once, since it automatically flushed and closed the stream. So I created classes that I called AutoFlushingStreamPumper and AutoFlushingPumpStreamHandler. The later is the same as a normal PumpStreamHandler but uses AutoFlushingStreamPumpers instead of the usual ones. The AutoFlushingStreamPumper does the same as a standard StreamPumper, but flushes its output stream every time it writes something to it.

    I've tested it pretty extensively and it seems to work well. Thanks to everyone who has tried to figure this out!

    0 讨论(0)
  • 2021-01-02 13:31

    For my purposes, it turns out I only needed to override the "ExecuteStreamHandler". Here is my solution, which captures stderr into a StringBuilder, and allows you to stream things to stdin and receive things from stdout:

    class SendReceiveStreamHandler implements ExecuteStreamHandler
    

    You can see the whole class as a gist on GitHub here.

    0 讨论(0)
提交回复
热议问题