How to wait till the end of DOS program started using Runtime.exec(cmd)?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chenthil
    New Member
    • Jan 2007
    • 2

    #1

    How to wait till the end of DOS program started using Runtime.exec(cmd)?

    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.getRunt ime().exec("cmd /c start .\\scripts\\cal l.bat");
    p.waitFor();
    System.out.prin tln("Exit value "+p.exitValue() );
    \\Start the second batch program run.bat
    Process p1 = Runtime.getRunt ime().exec("cmd /c start .\\scripts\\run .bat");
    }
    catch (Exception e) {
    e.printStackTra ce();
    }

    For this piece of code it starts the first batch program(i.e, call.bat) in a command prompt and immediately it starts the second batch program (i.e, run.bat) in another command prompt. So it runs both the batch programs simultanesously . But what I wanted is that my program should wait till the first batch is executed and then start the second batch.

    Please tell me how to wait between these two runtime commands in JAVA

    With regards,
    C.Chenthil.
    -------------------
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    I think it is because you are using the start command - remove it and you should be OK

    Comment

    • horace1
      Recognized Expert Top Contributor
      • Nov 2006
      • 1510

      #3
      alternativly if you need Command Prompt Windows try the /wait parameter to start, e.g.
      Code:
            Process p = Runtime.getRuntime().exec("cmd /c start/wait .\\scripts\\call.bat");
      the final command in the batch files should be exit

      Comment

      • chenthil
        New Member
        • Jan 2007
        • 2

        #4
        Originally posted by horace1
        alternativly if you need Command Prompt Windows try the /wait parameter to start, e.g.
        Code:
              Process p = Runtime.getRuntime().exec("cmd /c start/wait .\\scripts\\call.bat");
        the final command in the batch files should be exit

        Thanks a lot yar... This [Process p = Runtime.getRunt ime().exec("cmd /c start/wait .\\scripts\\cal l.bat");] worked fine.

        Thanks a lot for your timely help

        Comment

        • horace1
          Recognized Expert Top Contributor
          • Nov 2006
          • 1510

          #5
          Originally posted by chenthil
          Thanks a lot yar... This [Process p = Runtime.getRunt ime().exec("cmd /c start/wait .\\scripts\\cal l.bat");] worked fine.

          Thanks a lot for your timely help
          good to hear it worked - I find the following site useful for Windows command line infotrmation
          http://www.microsoft.c om/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds.mspx?mfr =true

          Comment

          • aabhijit
            New Member
            • Jul 2008
            • 1

            #6
            Thank you very much chenthil
            Your reply of cmd /c /start/wait worked perfectly alright.
            Thanks a ton,

            Comment

            Working...