• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Tim Cooke
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
Saloon Keepers:
  • Piet Souris
Bartenders:

Reading Process Instance Output

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm following some examples of using the Java Process class and noticed that when reading the output from the process execution you get the input stream and pass it to a buffered reader. I get the buffered reader (maybe not), but why pass the input stream and not the output stream? It seems counter intuitive to me. Since you use the input stream to get the output from the process execution, what is in the output stream? What's in the error stream?

Thank you.
 
Sheriff
Posts: 28536
114
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "output stream" contains the regular output from the Process. (For example if the Process were running some Java code, anything from "System.out.println" would go to that output stream.)

Now if you want to get a hold of data in that output stream, you're going to want to read from the output stream. And when you read from some source, whatever it may be, you need an InputStream to read from it. So in this case you need an InputStream to read from the Process's output stream. It's true that the names of those things can be distracting.

Oh yeah, what's in the "error stream"? If the Process were running Java code, anything from "System.err.println" goes to the error stream. You can read the data written to the error stream of the Process by... you know this now, right?... by using an InputStream which is connected to it.

 
Michael Craghead
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the response. It was helpful.

So to follow-up on one additional part, if I wanted to read the input stream from that process, that would be the input passed to the process instance, I would still need an input stream to read it or do I need an output stream to read the input stream?

Thanks again.
 
Bartender
Posts: 29139
215
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Create an OutputStream in your Java code to feed to the external program's stdin. Create InputStreams in your Java code to receive the external program stout and stderr.

It's all just a matter of setting up the proper plumbing.
 
Michael Craghead
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
 
Paul Clapham
Sheriff
Posts: 28536
114
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's probably already clear, but a Process is an externally running program which was created and started by your Java program. When your program creates a Process, sometimes it's just "fire and forget" but often you want to control that Process.

So think of the Process as being a program which runs at the command line. If you use the command line then you can type there, and then what you type will be treated as input by that program. This source of input is called "stdin" for "standard input". And perhaps you want your Java program, the one which created the Process, to control it by typing at the command line. You do that by connecting to the Process's standard input and writing to it, via that OutputStream which we already talked about. Your output is the Process's input.

Then the Process might respond by writing to the command line, which is called "stdout" for "standard output". Your Java program would want to read that response, which it does by connecting to the Process's standard output and reading from it.

There's also "stderr", for "standard error", which is where the program writes error-related data, usually error messages. At the command line, stdout and stderr both get written there, it's first come first served so they are just mixed together. But under the covers they are separate streams and your Java program can read them separately.

If you're used to running things at the command line then this should all be more or less familiar. But if you haven't ever done that then it might be less obvious. Back in the ancient past programmers always learned how to run things at the command line before starting to write code (because you had to know how to type the line to compile a program with all of its obscure options), but now you can be a programmer for years without ever having used a command line.
 
Author
Posts: 311
13
Scala IntelliJ IDE Netbeans IDE Python Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You probably have this straight by now, but think of your telephone. You speak into a microphone (InputStream) and the other person hears you through a speaker (OutputStream)...

And vice-versa.

HTH,
Simon
 
Tim Holloway
Bartender
Posts: 29139
215
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Simon Roberts wrote:You probably have this straight by now, but think of your telephone. You speak into a microphone (InputStream) and the other person hears you through a speaker (OutputStream)...

And vice-versa.

HTH,
Simon


More like a stereo telephone. It has 2 output channels and they don't play the same sounds.
 
reply
    Bookmark Topic Watch Topic
  • New Topic