ProcessHandler will redirect the output and error streams of the Process to the listener:
ProcessHandler can use a Process or a ProcessBuilder. In the case where a ProcessBuilder has been used, the ProcessHandler will create the Process when starting the handlerProcess.
ProcessBuilder pb = new ProcessBuilder("my_executable.exe"); ProcessHandler phandler = new ProcessHandler(pb, true);
ProcessHandler:Process already exist (depending on the constructor used too create the ProcessHandler)Process (if it does not already exists (depending on the constructor used too create the ProcessHandler), and start the handlerProcessBuilder pb = new ProcessBuilder("my_executable.exe"); try { Process process = pb.start(); ProcessHandler phandler = new ProcessHandler(process); List<String> lines = new ArrayList<> phandler.setListener(new ProcessHandler.ProcessListener() { @Override public void readOut(String line) { lines.add(line); } @Override public void readOut(String line) { } @Override public void close() { } }); } catch (IOException e) { } phandler.start();And with a ProcessBuilder:
ProcessBuilder pb = new ProcessBuilder("my_executable.exe"); try { ProcessHandler phandler = new ProcessHandler(pb); List<String> lines = new ArrayList<> phandler.setListener(new ProcessHandler.ProcessListener() { @Override public void readOut(String line) { lines.add(line); } @Override public void readOut(String line) { } @Override public void close() { } }); } catch (IOException e) { } phandler.start();
InputStream, which means that if the stream receives a lot of inputs in a short time, some of them could be discarded by the listener, leading to problems.Queue, which ensures that no event in the InputStream readings will be discarded:InputStream reading events and use them in the listener in another Thread. Between each even, there will be a delay of x ms, and a polling of y msInputStream reading events and use them in the listener in another Thread. Between each even, there will be a delay of x msInputStream reading events and use them in the listener in another Thread, with no delay
Queue, which ensures that to event in the OutputStream writings will be discarded:OutputStream writing events in another Thread. Between each even, there will be a delay of x ms, and a polling of y msOutputStream writing events in another Thread. Between each even, there will be a delay of x msOutputStream writing events in another Thread, with no delayProcessBuilder pb = new ProcessBuilder("my_executable.exe"); ProcessHandler phandler = new ProcessHandler(pb); List<String> lines = new ArrayList<> phandler.setListener(new ProcessHandler.ProcessListener() { @Override public void readOut(String line) { lines.add(line); } @Override public void readOut(String line) { } @Override public void close() { } }); phandler.start();
Copyright 2006-2024 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences