1

I am confused regarding use of input stream and output stream. From a stack overflow question confused about stdin, stdout and stderr? :

Standard input - this is the file handle that your process reads to get information from you.

Standard output - your process writes normal information to this file handle.

I was trying to run an external process(A python script) using java.Which is an interactive one and requires input from user.

Now as concept says that:

standard input is used when your process reads to get information from you.

So I should get input stream from the process to write values to it.I tried to it and failed .So I searched on stack overflow gave me a question with same problem which was taking output stream from process and then was writing to it.I tried that and that worked.

My question is that why that worked? Shouldn't it be input stream which I should use to give input to that external process or I have understood input stream and output stream totally wrong.

Please help me to understand it with an easy explanation .

Edit : My code was :

 Process process=Runtime.getRuntime().exec("/usr/bin/python /home/abhijeet/test.py");
    OutputStream  stdin = process.getOutputStream ();
    String line = "30" + "\n";
    stdin.write(line.getBytes() );
Community
  • 1
  • 1
Abhijeet Panwar
  • 1,837
  • 3
  • 26
  • 48

2 Answers2

2

An application should always write data onto the output stream of the parent process and read from the input stream of the parent process. For processes, whenever a child process is created, the parent process feeds data into the sub process input stream, and reads from the sub process output stream.

By default, the created subprocess does not have its own terminal or console. All its standard I/O (i.e. stdin, stdout, stderr) operations will be redirected to the parent process, where they can be accessed via the streams obtained using the methods getOutputStream(), getInputStream(), and getErrorStream(). The parent process uses these streams to feed input to and get output from the subprocess.

public abstract OutputStream getOutputStream()

Returns the output stream connected to the normal input of the subprocess. Output to the stream is piped into the standard input of the process represented by this Process object.

The application feeds data into Parent process output stream and that data will be piped on to the input stream of the sub process.

public abstract InputStream getInputStream()

Returns the input stream connected to the normal output of the subprocess. The stream obtains data piped from the standard output of the process represented by this Process object.

The parent process reads data from the output of the sub process. The Application reads the data from the parent process Input stream.

In your code:

Process process=Runtime.getRuntime().exec("/usr/bin/python /home/abhijeet/test.py");
OutputStream  stdin = process.getOutputStream ();
String line = "30" + "\n";
stdin.write(line.getBytes() );

The java program you run is the parent process. The python script you run is the child process. process.getOutputStream () returns you the parent process output stream. Don't get confused here. This method does not return the sub process output stream. Read the documentation i have quoted above carefully.

Now you write "30\n" to output stream of the parent process which is now piped into the python script process input stream, and becomes available to the script for reading.

BatScream
  • 19,260
  • 4
  • 52
  • 68
  • application should always read from the input stream of the parent process.Then why I need to take process's output stream to pass data to python script? – Abhijeet Panwar Sep 05 '14 at 10:18
  • Because to pass data to a sub process, you need to write data to the sub process input stream. So you need to write on to the parent process outputstream, which will be piped on to the sub process' input stream. I guess here your python script execution would be the sub process. – BatScream Sep 05 '14 at 10:23
  • I have edited question and have added respective code with it , can you please check and explain in brief .It will be very helpfull. – Abhijeet Panwar Sep 05 '14 at 10:31
  • Updated the answer wrt your comments – BatScream Sep 05 '14 at 10:38
  • Thanks a lot.I got clear with concept.I started this question because I was confused regarding use of input streams and output streams. I go the concept but still not able to solve that problem.I am using apache commons exec in which parent process is using satandard output for output.I could't apply it there.Have a look : http://stackoverflow.com/questions/25680210/how-to-run-interactive-python-script-using-apache-common-exec – Abhijeet Panwar Sep 05 '14 at 11:17
1

OutputStream as the name suggests, is for writing ouput to a stream (which might stream into a file but could also be a different type of stream).

InputStream likewise is used for reading input from a stream.

You could see those classes as opposite ends of the same stream: the producer/server writes to the output stream, the consumer/client reads from the input stream.

standard input is used when your process reads to get information from you.

That would describe the stream from the user's point of view. For you it is an output stream since you enter data using the console, for the process that reads that data, it is an input stream since it reads the data you entered into the stream.

A small visuablization:

User/Console                      Process/Application
(OutputStream) ------ data ----> (InputStream)
(InputStream)  <--- feedback --- (OutputStream)   
Thomas
  • 87,414
  • 12
  • 119
  • 157
  • 1
    You explained it right, but your visualization is wrong. For the application the stream it gets its data from is an inputstream. – kaetzacoatl Sep 05 '14 at 10:02
  • @Thomas : Thanks for your reply.It cleared difference for me. Now I am trying to do same with the apache common exec as in it sucprocess uses standard output stream (System.out).So should I just do System.out.write() and sub process will get it in it's input stream? I tried it, but that did't work – Abhijeet Panwar Sep 05 '14 at 10:10
  • @AbhijeetPanwar that depends on which input stream the process will read from. A process does not necessarily read from stdin. – Thomas Sep 05 '14 at 10:38