tags:

views:

47

answers:

2

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.

Basically, 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.getMessage());
  }
 }
+1  A: 

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

duffymo
A: 

Replace your while loop with this:

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