473,472 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 24476
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Avnish Midha | last post by:
Does the Runtime.exec() method support parameters from non-native locales i.e. does it really support the entire unicode range of characters in the parameters. I am trying ot invoke a C++...
1
by: TiscaliNews | last post by:
Hi, I have a tool that listen on a tcp/ip port and when a message arrives, it is printed to stdout. This tool is started using Runtime.exec using a wrapper to catch the output of the tool. The...
1
by: Hal Vaughan | last post by:
I've been using Runtime.exec() like this: Runtime rt = Runtime.getRuntime(); try {Process p = rt.exec("MyCommand.bat");} catch (Exception e) {do stuff} When I start my Java classes, I start...
0
by: Anastasios Kotsikonas | last post by:
Hi all, here's the situtation: 1) JDK 1.4.2_02 & Tomcat 4.1.x (no difference with any 1.4.x) 2) Multiple threads (testing with 5) access non-sync methods in a single instance of a class...
2
by: uwnewsgroup | last post by:
When I was using Runtime.exec(String cmd) to run a unix utility (join), and try to get its standard output by using Process.getOutputStream(), it blocks forever. I tried it using Java 1.4 and...
2
by: James Goldwater | last post by:
I have a situation where I actually _do_ want the command window to show after a runtime.exec() call. (Although we pipe stout/stdin to our own logging mech, the program needs to be able to trap...
5
by: chenthil | last post by:
In my Java program I need to call two DOS batch programs namely call.bat and start.bat. First I need to start the batch program call.bat and once that program is completed, I need to call the other...
8
by: angelotti | last post by:
Hi everyone , i am banging my head over the following problem for a couple of weeks now: i am starting a MyProg.exe(a simple C app) from a java class with .exec() as a system process(not in it's own...
5
by: madhawa84 | last post by:
i have written a simple java program using a Runtime.exec() .i was able to run my java program in the pc which i used to compiled the code.but when i migrate my program in to an another machine...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.