0

I built an interactive EXE which means that you can continuously send new commands to it and it will process them.

An automation of this can be implemented in Java according to this answer. However, when sending the command, the code will not wait till the command has finished. Instead, it will return the control back to the caller right away which might lead to race conditions: If the sent command was supposed to write a file, maybe the file isn't created yet before it is accessed. How can a command be sent, the output read and as soon as some input command is expected again, the sendCommand() call returns?

public synchronized void sendCommand(String command) throws IOException
{
    byte[] commandBytes = (command + "\n").getBytes(UTF_8.name());
    outputStream.write(commandBytes);
    outputStream.flush();
}

Preferably also returning the process output in the meantime. This would be the default behavior of a non-interactive shell command which terminates once finished executing. read() blocks indefinitely until the process terminates and I do not want to hardcode the length of the expected process output or similar hacks to circumvent this shortcoming.

BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185
  • Well, normal shell displays prompt when it is ready for next command... – hyde Jun 17 '19 at 05:12
  • @hyde: How do I wait for the prompt using `ProcessBuilder`? – BullyWiiPlaza Jun 17 '19 at 13:03
  • How do you wait for the prompt when you are using a terminal? You look at what is printed on terminal. In this case you need to read what is written by the launched process, and send it new command only after it reports that previous has completed. – hyde Jun 17 '19 at 14:32

1 Answers1

0

I decided to rewrite my binary to be non-interactive again. It turns out the expected performance gain was negligible so there was no more reason to keep it interactive and go through an increased implementation hassle.

BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185