473,915 Members | 5,000 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 24593
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
2155
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 UTF-16 java String parameters to the Runtime.exec() function, but when these parameters are checked in...
1
2334
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 come always available at the time it is expected. The wrapper receives the output from the tool in...
1
5089
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 has spaces in it, but I'm not sure if that's really the problem), with just the simple short batch...
0
1818
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
11081
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 the buffer allocated for that process. So can I adjust the buffer size (on any
2
2960
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+ Sun have 'fixed' it, and the dos window no longer pops up on a...
5
25247
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 call.bat Process p = Runtime.getRuntime().exec("cmd /c start .\\scripts\\call.bat"); ...
8
9622
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. The problem i meet is that the I/O is bloked while the process is being executed.When i terminate...
5
2614
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
9881
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11354
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
11066
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9732
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8100
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5943
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3368
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.