By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,269 Members | 1,332 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,269 IT Pros & Developers. It's quick & easy.

Is Runtime.exec() ALWAYS Asynchronous?

P: n/a
I never noticed this before, but when I run an external program through
Runtime, like this:

Runtime rt = Runtime.getRuntime();
System.out.println("Running external command: " + sCommand);
try {Process p = rt.exec(sCommand);} catch (Exception e)
{System.out.println("Cannot Run Command: " + sCommand + ", Error: " + e);}

the external command is asynchronous -- completely spun off as a separate
thread or process. Is there any way to avoid this? I have an external
command that creates a number of files. Once they're created by the other
program, I want to use them in my system. Is there a way to wait on a
Runtime object until the program finishes running? Or a way to check on it
and see if it is completed?

I figure I could also add in a loop to wait until the Runtime commands
create certain files (or alter others), but I can forsee more complications
with that than with some way to wait until the command is done.

I searched the Java SDK docs, but can't find methods in Runtime that will
help.

Thanks for any help.

Hal
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hal Vaughan wrote:

[...]
Is there a way to wait on a
Runtime object until the program finishes running? Or a way to check on
it and see if it is completed? [...] I searched the Java SDK docs, but can't find methods in Runtime that will
help.


See the Javadoc for java.lang.Process.

Process p = Runtime.exec("foo");
int exitCode = p.waitFor();

--
Jonas Kongslund
Jul 17 '05 #2

P: n/a
Jonas Kongslund wrote:
Hal Vaughan wrote:

[...]
Is there a way to wait on a
Runtime object until the program finishes running? Or a way to check on
it and see if it is completed?

[...]
I searched the Java SDK docs, but can't find methods in Runtime that will
help.


See the Javadoc for java.lang.Process.

Process p = Runtime.exec("foo");
int exitCode = p.waitFor();


That's exactly what I need. I started w/ Java a few months ago and have
NEVER used OOP or any kind of programming that deals with classses or
objects before (I used to program on an Apple //e in Assembler!), so I'm
still getting used to working with different classes and objects. While it
makes perfect, simple, sense to check the object created by the command,
I'm still not used to thinking that way.

Thanks. This helps with the immediate problem, but also helps me a little
with getting used to how Java "thinks" and how to use objects.

Hal
Jul 17 '05 #3

P: n/a
Hal Vaughan <ha*@thresholddigital.com> wrote in message news:<DVmpb.91355$Tr4.252674@attbi_s03>...
I never noticed this before, but when I run an external program through
Runtime, like this:

Runtime rt = Runtime.getRuntime();
System.out.println("Running external command: " + sCommand);
try {Process p = rt.exec(sCommand);} catch (Exception e)
{System.out.println("Cannot Run Command: " + sCommand + ", Error: " + e);}
Hal


hi

Process p = rt.exec(sCommand);
p. waitFor();
System.out.println("program exited with : " +p.exitValue());
is it this you are looking for ?
this will wait untill your command is finished executing & then you
can also access its exit value , if required

regards
amey
Jul 17 '05 #4

P: n/a
Hal Vaughan <ha*@thresholddigital.com> wrote in message news:<DVmpb.91355$Tr4.252674@attbi_s03>...
[snipped...]
I searched the Java SDK docs, but can't find methods in Runtime that will
help.

Process.waitFor() looks like it may do the job.
-FISH- ><>
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.