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);
}
精彩评论