I'm launching a process in the following way.
try {
final Process mvnProcess = new ProcessBuilder("cmd", "/c", "mvn", "--version")
.directory(new File(System.getProperty("user.dir")))
.inheritIO()
.start();
System.exit(mvnProcess.waitFor());
} catch (final IOException ex) {
System.err.format(IO_EXCEPTION);
System.exit(1);
} catch (final InterruptedException ex) {
System.err.format(INTERRUPTED_EXCEPTION);
System.exit(1);
}
Since I invoke inheritIO()
I was expecting the sub-process's output on the console, but nothing appears. What am I missing here?
Edit: I know that I can use mvnProcess.getInputStream()
and read the process's output explicitly, writing it to the console (or where-ever) in a loop. I don't like this solution however, since the loop will block my thread. inheritIO()
looked promising, but apparently I don't understand how it works. I was hoping someone here could shed some light on this.
Maybe it is an option the read it from the subprocess:
Add this code after start()
and you will have it printed to stdout:
InputStream is = mvnProcess.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null)
{
System.out.println(line);
}
You can use the .redirectError(Redirect.INHERIT). It sets the source and destination for sub process standard I/O to be the same as those of the current Java process.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With