Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can I make java wait / pause until Bat/Exe has completed?

1,997 views
Skip to first unread message

Mark Fitz

unread,
Mar 7, 2002, 4:11:57 PM3/7/02
to
I need my java program to wait until a bat file I call using
Runtime.getRuntime().exec( commandString )
has been completed.

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.

Gordon Beaton

unread,
Mar 7, 2002, 4:24:10 PM3/7/02
to

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

Roedy Green

unread,
Mar 7, 2002, 10:06:06 PM3/7/02
to
On 7 Mar 2002 13:11:57 -0800, ire...@yahoo.com (Mark Fitz) wrote or
quoted :

>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

Mark Fitz

unread,
Mar 11, 2002, 4:31:08 PM3/11/02
to
Thanks.

> 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.

Gordon Beaton

unread,
Mar 12, 2002, 12:01:14 AM3/12/02
to
On 11 Mar 2002 13:31:08 -0800, Mark Fitz wrote:
> 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.

0 new messages