Exec() problems on Windows

M

MMilkin

Im trying to automate some setupstuff
However im having problems doing this on Windows It seems that the file
gets entered and the first while loop returns all the errors that could
popup. However the second loop is not returning any output that is
occuring and the program is stalling.


proc = rt.exec("Blah.bat");
stderr = proc.getErrorStream();
isr = new InputStreamReader(stderr);
br = new BufferedReader(isr);


iostr = proc.getInputStream();
ist = new InputStreamReader(iostr);
bri = new BufferedReader(ist);

while ( (line = br.readLine()) != null)
{
myString = line;
System.out.println("Error:" + line + ":Error");
}

line = null;

while ( (line = bri.readLine()) != null)
{
myString = line;
System.out.println("Error:" + line + ":Error");
}


System.out.println("waiting for proc");
exitVal = proc.waitFor();

if(exitVal > 0)
{
System.out.println("exitVal: " + exitVal);
}
 
M

MMilkin

Wow never mind (Im sometimes dense) i think i figured it out I needed
to loop on output string inside the first loop.

-Mike
 
G

Gordon Beaton

Wow never mind (Im sometimes dense) i think i figured it out I
needed to loop on output string inside the first loop.

What you actually need to do is read from both streams concurrently,
i.e. while the process is still running. You will eventually run into
problems if you attempt to read the streams sequentially as you've
shown here, since the process could block while writing to the stream
you haven't started reading yet.

If you use ProcessBuilder you can combine both streams and don't need
the extra Thread to read them.

/gordon
 
N

Nigel Wade

Im trying to automate some setupstuff
However im having problems doing this on Windows It seems that the file
gets entered and the first while loop returns all the errors that could
popup. However the second loop is not returning any output that is
occuring and the program is stalling.


proc = rt.exec("Blah.bat");
stderr = proc.getErrorStream();
isr = new InputStreamReader(stderr);
br = new BufferedReader(isr);


iostr = proc.getInputStream();
ist = new InputStreamReader(iostr);
bri = new BufferedReader(ist);

while ( (line = br.readLine()) != null)
{
myString = line;
System.out.println("Error:" + line + ":Error");
}

line = null;

while ( (line = bri.readLine()) != null)
{
myString = line;
System.out.println("Error:" + line + ":Error");
}


System.out.println("waiting for proc");
exitVal = proc.waitFor();

if(exitVal > 0)
{
System.out.println("exitVal: " + exitVal);
}

The problem you are running into is the normal catch22 situation with Process
and reading the stdout/stderr streams. You are reading the error stream until
it closes (i.e. the process completes), then you read the output stream. If the
output stream fills the buffer and blocks the process, the process won't exit
so the first while loop won't terminate. If you reverse the order in which you
read the streams it doesn't solve the problem, it only reverses the failure
mode.

What you need to do is read both streams in parallel, and this normally requires
at least 2 threads. Which thread reads which stream, and what the thread does
with the data, is totally dependent on the expected data and how it needs to be
processed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top