1

I was calling a bat file from java which contains the executing command of a file.

ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/k","start test.bat");
Process p = builder.start();

I want to terminate this process after 3s. What's the way of setting time of execution for this process?

fardinabir
  • 124
  • 8
  • Just use a `Timer` and destroy the process when the timer is triggered. – AxelH Apr 11 '19 at 05:39
  • You can also use Cron jobs as well. – Jinesh Shah Apr 11 '19 at 05:41
  • Ideally, you should use `Timer` to run the job and then use `Thread.sleep(1000L * 2); timer.cancel();` to stop it for the desired interval. – darshgohel Apr 11 '19 at 05:51
  • `timer.cancel();` doesn't terminate the program, it just guarantees that the ongoing task execution is the last task execution that will ever be performed by this timer. Then what's the way? – fardinabir Apr 11 '19 at 11:45
  • do you have any idea? @darshgohel – fardinabir Apr 11 '19 at 15:31
  • The question you are suggesting, doesn't contain any exact solution for terminating the process. That shows exception and guarantees that the ongoing task execution is the last task execution. Please have a look.. @AxelH – fardinabir Apr 11 '19 at 17:27
  • 1
    No but the timer will be used to called `p.destroy()` after a specific amount of time... Since your question is to do something after 3s, it seems correct to point you to this duplicate. Of course, you still need to connect the dots. – AxelH Apr 12 '19 at 05:13
  • 1
    @fardinabir, you need to identify the solutions based on the comments, as your requirements are not much clear. In the above comments, there are a lot of hints which can actually solve your problem, but you have to identify which suits your requirement. – darshgohel Apr 12 '19 at 05:38

0 Answers0