473,573 Members | 2,814 Online
Bytes | Software Development & Data Engineering Community
+ 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.getRunt ime();
System.out.prin tln("Running external command: " + sCommand);
try {Process p = rt.exec(sComman d);} catch (Exception e)
{System.out.pri ntln("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 24517
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.Proce ss.

Process p = Runtime.exec("f oo");
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.Proce ss.

Process p = Runtime.exec("f oo");
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*@thresholdd igital.com> wrote in message news:<DVmpb.913 55$Tr4.252674@a ttbi_s03>...
I never noticed this before, but when I run an external program through
Runtime, like this:

Runtime rt = Runtime.getRunt ime();
System.out.prin tln("Running external command: " + sCommand);
try {Process p = rt.exec(sComman d);} catch (Exception e)
{System.out.pri ntln("Cannot Run Command: " + sCommand + ", Error: " + e);}
Hal


hi

Process p = rt.exec(sComman d);
p. waitFor();
System.out.prin tln("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*@thresholdd igital.com> wrote in message news:<DVmpb.913 55$Tr4.252674@a ttbi_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
2139
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++ application (exe) using the exec() method. The C++ application is unicode enabled i.e. it can accept wide chars as command line arguments. I am passing...
1
2321
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 wrapper is started and accessed from a jython script. Problem: The output from the tool (catched by the wrapper and displayed in jython) does not...
1
5062
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 them with a batch file that changes to my apps home directory. I've tried exec() with a full pathname (which seems to have problems on Windows if it...
0
1790
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 with ThreadLocals as appropriate (turns out using different instances of the class sans ThreadLocal doesn't change anything)
2
11066
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 the results are the same on SunOS 5.8, Redhat8.0, cygwin. I read relevant articles and find the reason is that the output of the process exceeds...
2
2949
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 Ctrl-C keystrokes). Our application is launched by javaw. In Java 1.3.1, this behaviour was present (and much resented it looks like) but in 1.4+...
5
25215
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 batch file start.bat. The piece of code which I am using is: public static void ExecuteScripts(){ try { \\Start the first batch program...
8
9587
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 console) and i would like to communikcate with its I/O before it is finished. I don't have this issue if for instance i am starting another java app....
5
2601
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 having the same OS(windows XP),my code did not worked .there was not any signs of execution of my code.why this was occured .explain me.
0
7784
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8204
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7795
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6424
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5600
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3734
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3735
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2223
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 we have to send another system
0
1044
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.