开发者

Losing Data from bash Script in Java

开发者 https://www.devze.com 2023-01-06 15:09 出处:网络
I am trying to get bash data into a variable. The problem is that it\'s so darn random.The command executes every time, I can see that by having an X application start.However my processor may be too

I am trying to get bash data into a variable. The problem is that it's so darn random. The command executes every time, I can see that by having an X application start. However my processor may be too fast or slow to issue the echo command and start a buffered read to the input stream.

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

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.getMessag开发者_高级运维e());
  }
 }


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


Replace your while loop with this:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号