The bat file creates an ouput file which I then want my java program
to parse but the program tries to parse the new file before it is
actually created.
How can I tell when the bat file/process has been completed so I can
continue with the remaining part of the program?
Any ideas appreciated.
Regards,
Fitzy.
Process.waitFor() will cause the calling thread to block until the
external process has finished.
However if your external program can write the results to stdout
instead of to a file, then Java can read the contents directly from
Process.getInputStream() as it is created, making both the file and
the need to wait for it completely unnecessary.
/gordon
--
[ do not send me private copies of your followups ]
g o r d o n . b e a t o n @ e r i c s s o n . c o m
>I need my java program to wait until a bat file I call using
>Runtime.getRuntime().exec( commandString )
>has been completed.
See exec in the Java glossary. For Windows see the START command for
use in your BAT file.
The java glossary is at
http://www.mindprod.com/gloss.html
or http://209.139.205.39
--
eagerly seeking telecommuting programming work.
canadian mind products, roedy green
> Process.waitFor() will cause the calling thread to block until the
> external process has finished.
>
> However if your external program can write the results to stdout
> instead of to a file, then Java can read the contents directly from
> Process.getInputStream() as it is created, making both the file and
> the need to wait for it completely unnecessary.
>
Here's what I had to do...
String commandString = new String( "cmd /c d:\\tom\\tidyhtm2.bat >>
mmm.txt" );
Process p = Runtime.getRuntime().exec( commandString );
try
{
p.waitFor();
}
catch(InterruptedException ie)
{
System.out.println("Your Exception Code is " + ie);
}
What I found was that if I didn't have the output to mmm.txt the
program seemed to hang. No idea why.
Most likely because you weren't reading the output. If an external
program generates any significant amount of output (more than a few
hundred characters) then the stream will eventually fill, causing the
process to block while it waits for someone (in this case, your Java
application) to read from the other end and make room for new output.
So it was waiting for you, and you were waiting for it, a state known
as deadlock.