I have a string that I need to pipe into an external program, then read the output back. I understand how to read the output back, but how do I pipe this string as input? Thanks!
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Careful that you don’t create a deadlock. Reading/writing to the process in the same thread can be problematic if the process is writing data you are not reading and meanwhile you are writing data it is not reading.
I tend to use a little pattern line this to get the io going in different threads:
This may or may not apply to what you want to do — maybe your dealing with an established set of input that is guaranteed to show up after certain expected output (or vice versa, or several iterations of that). I.e. some synchronous block and step kind of interaction.
But if you are simply ‘watching’ for something that might show up then this is a good start in the right direction. You would pass in some different streams and work in some java.util.concurrent ‘await(long,TimeUnit)’ type code for waiting for the response. Java IO blocks basically forever on read() ops, so separating yourself from those threads will allow you to give up after a certain time if you don’t get the expected response from the external process.