(I'm new to java) I need to start the process and get 2 or 3 descriptors: for STDIN, STDOUT (and STDERR), so I can write the input to the process and get its output in the same way as command line lines behave (for example, grep ) / p>
in Python, this is achieved with the following code:
from subprocess import Popen, PIPE
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE)
(child_stdin, child_stdout) = (p.stdin, p.stdout)
child_stdin.write('Yoram Opposum\n')
child_stdin.flush()
child_stdout.readlines()
What is the Java equivalent?
I tried so far
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader inp = new BufferedReader( new InputStreamReader(p.getInputStream()) );
BufferedWriter out = new BufferedWriter( new OutputStreamWriter(p.getOutputStream()) );
out.write( "Some Text!\n\n" );
out.flush();
line = inp.readLine();
print("response1: " + line );
out.write( "Second Line...\n" );
out.flush();
line = inp.readLine();
print("response2: " + line );
inp.close();
out.close();
By the way, the first attempt only works with \ n \ n, but does not work with one \ n (why?)
The following code works, but all the data is entered in advance, and not the behavior I'm looking for:
out.write( "Aaaaa\nBbbbbb\nCcccc\n" );
out.flush();
line = inp.readLine();
print("response1: " + line );
line = inp.readLine();
print("response2: " + line );
line = inp.readLine();
print("response3: " + line );
line = inp.readLine();
print("response4: " + line );
output:
response1: AAAAA
response2:
response3: bbbbbb
response4:
the process being performed is as follows:
s = sys.stdin.readline()
print s.upper()
s = sys.stdin.readline()
print s.lower()