I'm trying to use ProcessBuilder for something as simple as an 'ls' command. I already read the issues about the process streams required to be consummed before the waitFor() call returns, but even with error redirected to output and consumming the stream, the process never returns.
public class ProcessTest {
public static void main(String[] args) throws Exception {
ProcessBuilder builder = new ProcessBuilder();
builder.command("ls");
builder.redirectErrorStream(true);
Process process = builder.start();
StreamGobbler streamGobbler = new StreamGobbler(process.getInputStream(), System.out::println);
Executors.newSingleThreadExecutor().submit(streamGobbler);
int exitCode = process.waitFor();
}
}
with StreamGlobber beeing a naive consummer :
public class StreamGobbler implements Runnable {
private InputStream inputStream;
private Consumer<String> consumer;
public StreamGobbler(InputStream inputStream, Consumer<String> consumer) {
this.inputStream = inputStream;
this.consumer = consumer;
}
@Override
public void run() {
new BufferedReader(new InputStreamReader(inputStream)).lines().forEach(consumer);
}
}
can someone show me what's i'm going wrong here ?