Losing data from bash Script in Java

I am trying to get bash data into a variable. The problem is that this is so darn randomly. The command is executed every time, I see that the application X is starting up. However, my processor may be too fast or slow to issue the echo command and start the buffered read to the input stream.

How can I make this work? I need to somehow issue a command inside a buffered reader.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class test1 {

 public static void main() {

  try { 
   Process proc = Runtime.getRuntime().exec("echo Gosh, I sure hope this comes back to java");
   BufferedReader read = new BufferedReader(new InputStreamReader(proc
     .getInputStream()));

   while (read.ready()) {
    System.out.println(read.readLine());
   }
  } catch (IOException e) {
   System.out.println(e.getMessage());
  }
 }
+3
source share
2 answers

Replace the while loop as follows:

String line;
while ((line = read.readLine())!=null) {
    System.out.println(line);
}
0
source

Either read this , or try java.lang.Process .

+1
source

Source: https://habr.com/ru/post/1754156/


All Articles