I am trying to invoke a batch file through a tomcat application. The batch is executed properly, but the timeout command in the batch does not pause the batch. When a batch is executed directly, the timeout does pause the process as expected.
I am invoking batch as: Runtime.getRuntime().exec("test.bat");
When you call Runtime.getRuntime().exec()
the input and output streams of the started process are redirected. timeout
being started from this process inherits the same handles.
The problem is that timeout
tries to get access to the console, something it can not do because of the redirection.
You can test this behaviour running from a console
<nul timeout /t 10
When the input stream is redirected, timeout
fails.
One workaround could be
( timeout /t 10 || >nul ping -n 11 localhost ) 2>nul
If the timeout
fails, a ping
command is executed to force the wait.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With