Viewing console output of exec right away [message #327050] |
Mon, 07 April 2008 16:47 |
Eclipse User |
|
|
|
Originally posted by: ups_genius.gmx.net
Hi everyone!
I am pulling my hair our over this problem, I hope somebody can help me, I
just can't get the right idea how to do it.
I would like to write the output of a batch file executed via
Runtime.getRuntime().exec(cmd); to the console. This alone is not the
problem. I manage to write the output to the console whenever the batch
file execution is finished. But since this takes quite a while and the
file produces quite a bit of output, I would like to send the output to
the console right away, whenever it is produced.
I tried created the following class for the stdout and stderr threads, but
the console content only changes whenever the batch is finished and the
console then suddenly contains the complete content.
The "System.out.println" in the line below though does the job correctly,
writing line by line.
Can anyone help me? What am I doing wrong?
Thank you very much!
Christian
// Console for stderr and stdout
MessageConsole console = findConsole("Batch Output");
//out = console.newMessageStream();
MessageConsoleStream stream = console.newMessageStream();
// any error message?
StreamReader errorReader = new StreamReader(proc.getErrorStream(), stream);
// Handle input (java plugin input == batch file output)
StreamReader outputReader = new StreamReader(proc.getInputStream(),
stream);
class StreamReader extends Thread
{
InputStream is;
String type;
MessageConsoleStream stream;
StreamReader(InputStream is, MessageConsoleStream stream)
{
this.is = is;
this.stream = stream;
}
public void run()
{
try
{
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ( (line = br.readLine()) != null)
{
stream.println(line);
System.out.println(line);
}
br.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
|
|
|
Re: Viewing console output of exec right away [message #327055 is a reply to message #327050] |
Mon, 07 April 2008 20:43 |
Eclipse User |
|
|
|
Originally posted by: wegener.cboenospam.com
"Christian" <ups_genius@gmx.net> wrote in message
news:026dec537341a4689486383c407b2757$1@www.eclipse.org...
> Hi everyone!
>
> I am pulling my hair our over this problem, I hope somebody can help me, I
> just can't get the right idea how to do it.
>
> I would like to write the output of a batch file executed via
> Runtime.getRuntime().exec(cmd); to the console. This alone is not the
> problem. I manage to write the output to the console whenever the batch
> file execution is finished. But since this takes quite a while and the
> file produces quite a bit of output, I would like to send the output to
> the console right away, whenever it is produced.
> I tried created the following class for the stdout and stderr threads, but
> the console content only changes whenever the batch is finished and the
> console then suddenly contains the complete content.
> The "System.out.println" in the line below though does the job correctly,
> writing line by line.
> Can anyone help me? What am I doing wrong?
>
> Thank you very much!
>
> Christian
>
>
> // Console for stderr and stdout
> MessageConsole console = findConsole("Batch Output");
> //out = console.newMessageStream();
> MessageConsoleStream stream = console.newMessageStream();
>
>
> // any error message?
> StreamReader errorReader = new StreamReader(proc.getErrorStream(),
stream);
>
> // Handle input (java plugin input == batch file output)
> StreamReader outputReader = new StreamReader(proc.getInputStream(),
> stream);
>
>
>
> class StreamReader extends Thread
> {
> InputStream is;
> String type;
> MessageConsoleStream stream;
>
> StreamReader(InputStream is, MessageConsoleStream stream)
> {
> this.is = is;
> this.stream = stream;
> }
>
> public void run()
> {
> try
> {
> InputStreamReader isr = new InputStreamReader(is);
> BufferedReader br = new BufferedReader(isr);
> String line = null;
>
> while ( (line = br.readLine()) != null)
> {
> stream.println(line);
> System.out.println(line);
> }
> br.close();
>
> }
> catch (IOException ioe)
> {
> ioe.printStackTrace();
> }
> }
> }
>
Have you tried adding a stream.flush() after the stream.println. If the
OutputStream is buffered, you need to flush the stream in order to force the
data out.
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04726 seconds