You need to read the result (even if you just throw it away), both stdout and stderr. Otherwise, the output buffer will fill up, resulting in odd behavior.
The simplest approach would be to redirect to bit-bit (assuming Java 1.7 +):
File bitbucket;
if (isWindows()) {
bitbucket = new File("NUL");
} else {
bitbucket = new File("/dev/null");
}
Process process = new ProcessBuilder("/bin/bash", "-c", "./start_server")
.redirectOutput(ProcessBuilder.Redirect.appendTo(bitbucket))
.redirectError(ProcessBuilder.Redirect.appendTo(bitbucket))
.start();
But if you need to use Java 1.6 or lower, you will have to minimize it yourself. This is what I use in my projects:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class StreamConsumer
{
private final InputStream stream;
private StreamConsumer(InputStream is)
{
this.stream = is;
}
private Runnable worker()
{
return new Worker();
}
private class Worker implements Runnable
{
@Override
public void run()
{
try (BufferedReader br = new BufferedReader(new InputStreamReader(stream))) {
while (br.readLine() != null) {
}
} catch (IOException ioe) {
}
}
}
public static void consume(InputStream stream, String label)
{
Thread t = new Thread(new StreamConsumer(stream).worker(), label);
t.setPriority(Thread.MIN_PRIORITY);
t.start();
}
}
And you would call it that:
Process process = new ProcessBuilder("/bin/bash", "-c", "./start_server")
.start();
StreamConsumer.consume(process.getInputStream(), "STDOUT");
StreamConsumer.consume(process.getErrorStream(), "STDERR");
Or like this:
Process process = new ProcessBuilder("/bin/bash", "-c", "./start_server")
.redirectErrorStream(true)
.start();
StreamConsumer.consume(process.getInputStream(), "STDOUT/STDERR");
Note that the constructor ProcessBuilderaccepts String...(varargs), not String[](although you can create and pass String[]manually if you want)