Runtime exec Process hangs Runtime exec Process hangs kubernetes kubernetes

Runtime exec Process hangs


You are not consuming the stdout and stderr streams properly, they will cause process output to hang if one fills without you reading it. You could try setting stderr to go to stdout class when using ProcessBuilder

ProcessBuilder pb = new ProcessBuilder(new String[]{"kubetail", "serviceName"});pb.redirectErrorStream(true);Process exec = pb.start();... Your reading codeint rc = exec.waitFor();

OR: add threads to consume both stdout and stderr streams:

Process exec = Runtime.getRuntime().exec(new String[]{"kubetail", "serviceName"});        new Thread(() -> copy(exec.getInputStream(), System.out), "STDOUT").start();new Thread(() -> copy(exec.getErrorStream(), System.err), "STDERR").start();int rc = exec.waitFor();

with method:

static void copy(InputStream in, OutputStream out){    try(var autoClose = in; var autoClose2 = out)    {        in.transferTo(out);    }    catch(IOException io)    {        throw new UncheckedIOException(io);    }}