My app uses a process in separate thread to run some commands and get the input from them:
process = Runtime.getRuntime().exec("su");
out = new DataOutputStream(process.getOutputStream());
The app sends commands to the process like this:
public void setCommands(String[] commands)
{
try{
for(String command : commands){
out.writeBytes(command + "\n");
}
out.writeBytes("exit\n"); //if I comment this line the commands get lost
out.flush();
}catch(IOException e){
e.printStackTrace();
}
}
The thread then reads the input from process with BufferedReaders and sends it to the main thread and it works fine for the first time. The problem is that I want to reuse the same process with multiple calls to setCommands()
, but after the first call the OutputStream of the process gets closed with out.writeBytes("exit\n");
statement. If I comment this line it seems like the out.flush()
starts to have no effect. Could somebody please explain to me why is this happening and how can this be done right?