473,466 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

p.waitfor does not wait for process to finish before starting another process

74 New Member
Hi,

I'm creating a java program which will run other executable .jar files and the order of how these other jar files runs is important. I need to run 3 jar files. A.jar, B.jar, and C.jar. In my main program which will control the execution of my other jar files, this is what i have:

Expand|Select|Wrap|Line Numbers
  1. public static void main(String[] args) {
  2.  
  3. Program.processRequest(settings.ini)
  4.  
  5. }
  6. //the settings.ini file will have the list of commands to execute
  7.  
i then open the settings file and read the properties

Expand|Select|Wrap|Line Numbers
  1. String keyBase = "command";
  2.                 Enumeration he = props.keys();
  3.                 Process p;
  4.                 while (he.hasMoreElements())
  5.                 {
  6.                     String hKey = (String) he.nextElement();
  7.  
  8.                     //if the key matches "command" then get the property and create the file
  9.                     if (hKey.startsWith(keyBase))
  10.                     {
  11.                         String hName = props.getProperty(hKey);
  12.                         System.out.println(TimeNow.timeNow() + ": Executing Command: " + hName);
  13.  
  14.                         try {
  15.                             p = Runtime.getRuntime().exec("cmd /c start "+ hName);
  16.                             p.waitFor();
  17.  
  18.                         } catch (Exception e) {
  19.                             e.printStackTrace();
  20.                         }
  21.                     }
  22.                 }
  23.             }
  24.  
Basically, the loop searches the .ini file for the command, something like "java -jar A.jar settings.ini"
This works perfectly fine if i run each command separately, however, if i run the commands one after the other from within the loop, i.e:

Expand|Select|Wrap|Line Numbers
  1. p = Runtime.getRuntime().exec("cmd /c start "+ "java -jar A.jar");
  2. p = Runtime.getRuntime().exec("cmd /c start "+ "java -jar B.jar");
  3. p = Runtime.getRuntime().exec("cmd /c start "+ "java -jar C.jar");
  4.  
all 3 .jar files will run at the same time.Is there a way to execute the second jar file ONLY AFTER the first jar file has completed? Similarly, the third .jar file executes only AFTER the second one has completed? The reason i'm doing it this way is that in the future, if i need to add more jar files to run after the 3rd jar file, i do not have to modify any code and i can just add the command in the settings.ini file.

Maybe i'm missing something about how waitFor works, or how the Process class works? I have tried to find examples on google, but i was not as successful as i had hoped.

Any suggestions are greatly appreciated. Thank you!
Aug 31 '11 #1
0 1488

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Stephen Haeney via .NET 247 | last post by:
I am developing a WinForm application which will redirect theuser to HTML page, using IE, when they click a button. However,I want this to be modal in operation, so I would like to makesure that the...
22
by: codefixer | last post by:
Hi, I have a situation where I have to handle the following scenario. The main() must wait for child to complete or the main() must kill the child after 3 seconds and exit. /* Assume...
6
by: Saso Zagoranski | last post by:
Hi! I'm trying to run a decryption utility (des), which needs the following parameters: -D-u -k "key" input.file output.file If I run this from the command-prompt it works but when I try to...
3
by: Holger (David) Wagner | last post by:
I'm trying to start a process from an ASP.NET Web application. The reason I need this is to encapsulate a (unmanaged) DLL that sometimes crashes. If I run this DLL directly with P/Invoke from...
1
by: Daniel | last post by:
what permissions does a windows service need to execute another process? System.Diagnostics.Process process = System.Diagnostics.Process.Start(info); just local administrator? any specific...
5
by: xhy_China | last post by:
How to close an file in an running process from another process? For example: There are two running processes,named procIn and procFrom. A file named needCloseFile was opened in procIn. Now I...
2
by: Mario Beutler | last post by:
How can I check if a process a .net process or a "normal" win32 process? Mario
2
by: Fred Heida | last post by:
Hi, Question on Vista Run As Administrator. If i have exe which creates a child process, using the Process class, and this exe is Run As Asministror, is there a way to have the child process...
0
by: Dominic | last post by:
Every thing work fine when I am not starting process as another user. When I am starting process as another user, the authentication is working (I can see it in my workstation security log), but...
0
by: eashokan | last post by:
Hi.. I am able to get the process (as object) running in the sysetm through process id. I need to trigger the event that corresponds to the process (object) i got. Note that the Process object i...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...
0
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...
0
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,...
0
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...
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?

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.