• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

redirect conosle result  RSS feed

 
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.
 
(instanceof Sidekick)
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.
 
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 ]
 
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 ]
 
Sheriff
Posts: 21719
102
Chrome Eclipse IDE Java Spring Ubuntu VI Editor 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 >.
 
A lot of people cry when they cut onions. The trick is not to form an emotional bond. This tiny ad told me:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!