4

What is the difference between Runtime.getRuntime().exec() and double click to execute batch file?

when I execute batch file by using Runtime.getRuntime().exec()(case 1) like this,

String[] command = {"cmd.exe", "/C", "Start", "D:\\test.bat"};
Process p =  Runtime.getRuntime().exec(command);  

or

Process p = Runtime.getRuntime().exec("cmd /c start "+"D:\\test.bat")

a new cmd console window open, and launching test.bat.

and if I try to execute test.bat by mouse double click on my desktop(case 2), it also open in a new cmd console window, and launching application.

but problem is occurred when I try to stop test.bat. I using 'Ctrl-C' to stop batch file,

when I using 'Ctrl-C' in case 1, cmd console stop but still remain window and prompt line.

but when I using 'Ctrl-C' in case 2, cmd console stop and close the window!!

I want make close the window in case 1. but I don't know how.. how can I do that??

ps. I try to write 'exit' in test.bat, but it's not working.

0

1 Answer 1

2

It is not a problem with Runtime.exec() in java, you have the same problem if you open a cmd console and type your command. It's because start doesn't exit (whereas cmd with flag C exits when command is finished).

It doesn't work by adding exit at the end of test.bat because you killed by ctrl+c the execution of the script, and exit statement is not executed.

You could try to change your call as follow to fix the problem:

cmd /C start cmd /C test.bat
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.