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
- public static void main(String[] args) {
- Program.processRequest(settings.ini)
- }
- //the settings.ini file will have the list of commands to execute
Expand|Select|Wrap|Line Numbers
- String keyBase = "command";
- Enumeration he = props.keys();
- Process p;
- while (he.hasMoreElements())
- {
- String hKey = (String) he.nextElement();
- //if the key matches "command" then get the property and create the file
- if (hKey.startsWith(keyBase))
- {
- String hName = props.getProperty(hKey);
- System.out.println(TimeNow.timeNow() + ": Executing Command: " + hName);
- try {
- p = Runtime.getRuntime().exec("cmd /c start "+ hName);
- p.waitFor();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
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
- p = Runtime.getRuntime().exec("cmd /c start "+ "java -jar A.jar");
- p = Runtime.getRuntime().exec("cmd /c start "+ "java -jar B.jar");
- p = Runtime.getRuntime().exec("cmd /c start "+ "java -jar C.jar");
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!