Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Losing Data from bash Script in Java

Tags:

java

bash

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.getMessage());
  }
 }
like image 454
Adam Outler Avatar asked Jul 31 '26 10:07

Adam Outler


1 Answers

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

like image 142
duffymo Avatar answered Aug 02 '26 01:08

duffymo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!