472,145 Members | 1,442 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

Is Runtime.exec() ALWAYS Asynchronous?

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

Similar topics

reply views Thread by Avnish Midha | last post: by
1 post views Thread by TiscaliNews | last post: by
1 post views Thread by Hal Vaughan | last post: by
reply views Thread by Anastasios Kotsikonas | last post: by
2 posts views Thread by uwnewsgroup | last post: by
5 posts views Thread by madhawa84 | last post: by
reply views Thread by Saiars | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.