Winn
Winn

Reputation: 423

How to invoke multiple java programs in separate command window from single class using Runtime.exex() in Windows OS?

I am using Runtime.exex() something like this to invoke 2 different java programs in 2 separate command windows (in windows 7 environment).

   public class Invoke{
         public void main(...){
            String[] class1 = {"start", "java", "A"}; //Assume A.java is already compiled
            String[] class2 = {"start", "java", "B"}; //Assume B.java is already compiled
            try{
            Runtime.getRuntime().exec(class1);
            Runtime.getRuntime().exec(class2);
            }catch(Exception e){
               e.printStackTrace();
             }
         }
   }

But its giving me Exception

       java.io.IOException: Cannot run program "start": CreateProcess error=2, The syst
       em cannot find the file specified
    at java.lang.ProcessBuilder.start(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at initialConfig.StartApp.win_startProg(StartApp.java:95)
    at initialConfig.StartApp.main(StartApp.java:134)
     Caused by: java.io.IOException: CreateProcess error=2, The system cannot find th
     e file specified
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(Unknown Source)
    at java.lang.ProcessImpl.start(Unknown Source)

Do I need to create a separate .bat file to invoke these two programs and then use these bats here in Runtime.exec()? Please Help. Thanks.

Upvotes: 0

Views: 435

Answers (1)

halfbit
halfbit

Reputation: 3464

I think start is an internal command of cmd.exe. So try:

String[] class1 = {"cmd.exe", "/c", "start", "java", "A"};

This leads to invoking java in separate process and window - and will not wait for it to terminate.

Upvotes: 1

Related Questions