I have a program where I open a .exe file using the Process class in java (and Runtime). The function that does this returns the output of the exe file when a command is sent. Here is the method:
public String getOutput(int waitTime) {
StringBuffer buffer = new StringBuffer();
try {
Thread.sleep(waitTime);
sendCommand("isready");
for (;; ) {
String text = processReader.readLine();
if (text.equals("readyok")) {
break;
} else {
buffer.append(text + "\n");
}
}
}
catch (Exception e) {
e.printStackTrace();
}
return buffer.toString();
}
public void sendCommand(String command) {
try {
processWriter.write(command + "\n");
processWriter.flush();
}
catch (IOException e) {
e.printStackTrace();
}
}
Note: I am using outputStreamWriter and BufferedReader to read and write the data that the program outputs.
The only problem is that I have to run this code twice at once, but it pauses the thread making the program unresponsive. Is it possible to implement a second thread that I can use?