0

I'm using Sikuli to execute my automated tests, and I'm writing a small java app for running my tests. I've searched for other answers but most of them relate to running batch scripts from java, I need to run the entire command from java.I have the following command to run Sikuli from cmd:

Process p = Runtime.getRuntime().exec("cmd /c start /wait C:\\Sikuli\\runIde.cmd -r C:\\Sikuli\\xmlRunner\\runners\\Runner.sikuli --args 5");
System.out.println("Waiting for command to be executed");
p.waitFor();
System.out.println("Command finished");

This starts a new cmd window and starts executing my script but when the script finishes, it does not close the cmd window. Is there a way to automatically close the cmd window when my script finishes running?

stewazy
  • 139
  • 1
  • 3
  • 8
  • Possible duplicate of [close a particular command prompt](https://stackoverflow.com/questions/618528/close-a-particular-command-prompt) – Kaustubh Kallianpur Sep 20 '17 at 09:40
  • Did you try to chain `exit` after your command? Something like this: `Process p = Runtime.getRuntime().exec("cmd /c start /wait C:\\Sikuli\\runIde.cmd -r C:\\Sikuli\\xmlRunner\\runners\\Runner.sikuli --args 5 & exit");` – Azeem Sep 20 '17 at 09:53
  • Are you sure you want to see that console window? – talex Sep 20 '17 at 10:08
  • If you don't want it to wait, why are you specifying `/wait`? – user207421 Sep 20 '17 at 10:12

1 Answers1

1

Use cmd /c start /wait cmd /c C:\\Sikuli\\runIde.cmd -r C:\\Sikuli\\xmlRunner\\runners\\Runner.sikuli --args 5

See dock:

WAIT        Start application and wait for it to terminate.
command/program
            If it is an internal cmd command or a batch file then
            the command processor is run with the /K switch to cmd.exe.
            This means that the window will remain after the command
            has been run.

            If it is not an internal cmd command or batch file then
            it is a program and will run as either a windowed application
            or a console application.

When you directly call runIde.cmd the /K switch is applied, but when you proxy this call with cmd.exe this rule is not applicable.

talex
  • 17,973
  • 3
  • 29
  • 66
  • If I remove /wait from the command it will go to the line "Command finished" even though the script has not finished its run. – stewazy Sep 20 '17 at 09:48
  • 1
    Try `"cmd /c start /wait cmd /c C:\\Sikuli\\runIde.cmd -r C:\\Sikuli\\xmlRunner\\runners\\Runner.sikuli --args 5"` – talex Sep 20 '17 at 10:05
  • 1
    @stewazy I adjusted my answer. – talex Sep 21 '17 at 04:40