What I want to achieve :
I have a JAVA code which starts an ngrok(ngrok is used to expose your local ports to web so others can access it. More info here) process. Now after spawning new ngrok process using Runtime.getRuntime()
, I want to read it's output.
Problem :
When I try to read from InputStream
using getInputStream()
and getErrorStream()
, my program gets stuck there. So I'm unable to read output of process.
Code :
Process ngrokProcess = Runtime.getRuntime().exec(" cmd /k start cmd /k \" ngrok http 8080 \" ");
String collect = Stream.of(ngrokProcess.getInputStream(), ngrokProcess.getErrorStream()).parallel().map((InputStream is) -> {
StringBuilder output = new StringBuilder();
try (BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
String line;
while ((line = br.readLine()) != null) {
output.append(line);
output.append("\n");
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return output;
}).collect(Collectors.joining());
What am I doing wrong here? Or is there any other way in which I can achieve it?
I can't wait for process to finish as it's never ending unless terminated forcefully. Also, I can't use ProcessBuilder
as command prompt is not visible to user (I can be wrong here and need to tweak some things for it to visible. If it's possible let me know).
If more info is required let me know.
Thanks in advance :)