• Post Reply Bookmark Topic Watch Topic
  • New Topic

redirect conosle result  RSS feed

 
iyappan sankaran
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai
how to redirect the console result to a application.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One neat way is to run the command with runtime.exec and connect readers to the stdout and errout of the command. You can also connect a writer to the stdin of the program and interact with prompts and responses and such. You kinda need to get into some threading tricks to make it all work, so it's not quite a trivial task. Let us know if you'd like samples.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[ October 05, 2004: Message edited by: Stefan Wagner ]
 
Joaquin Morcate
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to capture the output of a process that I started using exec(), and I have observed a different behaviour between stdin and stderr.

I have de following C code: (HelloWorld.c)

and to capture output: (TheLaucher.java)


In this case I have to wait till the end of the process to get the output but if I use the stderr changing:


for


I get the strings as they are available.
How can I get the same behaviour with the stdout, i.e. to get all the messages as they are produced? Why are they different?

Thanks

Joaquin Morcate
[ November 17, 2005: Message edited by: Joaquin Morcate ]
 
Rob Spoor
Sheriff
Posts: 21048
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Processes can output to 2 distinct streams, stdout and stderr. While you see these two as one in a command box, they are still distinct. When reading one, you will not see the results of the other.

There are ways to redirect them, so that they both go to the same stream:
- in Java, use System.setOut(System.err) or System.setErr(System.out).
- if that is not possible, you can use OS redirection. After your command you add a 2>&1 to redirect the error stream to the output stream and >&2 for the other way around. E.g, "someCommand -paramater value <any other input to the command here> 2>&1". There cannot be any spaces around the >.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!