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

前端 未结 3 2135
孤街浪徒
孤街浪徒 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: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!

提交回复
热议问题