ProcessBuilder and Process.waitFor (), how long does it wait?

I am executing a .exe file from Java using the ProcessBuilder class and the Process class. To explain what I am doing:

builder = new ProcessBuilder(commands); builder.redirectErrorStream(true); Process process = builder.start(); process.waitFor(); 

I just wanted to know how long to wait for "waitFor ()"? Is it waiting for my .exe to be executed, or is it waiting for its execution to finish?

My .exe is a compiled AutoIt script. This means that there may be interactions such as mouse movements that take some time. Therefore, I need to know whether the execution of my Java code continues after calling .exe or whether it really waits for it.

What I want to achieve is the rotational execution of two scripts, but I'm afraid that my Java code is executing the second script while the first is still executing. Does anyone have a workaround for this? I am happy for any ideas.

+9
source share
3 answers

The current thread of execution will be blocked on process.waitFor() until the process is completed (that is, execution is completed). Source here

Also note that if the process has already completed: waitFor () will not be blocked. I don’t know if the code that you put in your question is exactly what you ran ... but you have to be careful and recreate a new instance of the process for every execution of your script (i.e. once in the same Process: it will not work after the first execution)

+8
source

In addition, if the "teams" have outputs. you should read the standard output and the standard error output using stream(process.getErrorStream()) and process.getInputStream()) . If not and the exit or exit errors are filled, waitfor() will hang.

+3
source

He will wait for the completion of the process. Workaround:

1 Use isAlive ()

2 Use waitFor (long timeout, TimeUnit unit) (1.8 only)

+2
source

Source: https://habr.com/ru/post/985509/


All Articles