0

To run the executable and get the real-time output I'm using the following code (with String[] command = new String[]{"/a.out"};):

public static void execute(String... command) {
    try {
        ProcessBuilder builder = new ProcessBuilder(command);
        builder.redirectErrorStream(true);
        final Process proc = builder.start();
        new Thread(new Runnable() {
            public void run() {
                BufferedReader reader = null;
                try {
                    reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        EventBus.getDefault().post(new MessageEvent(line+"\n"));
                    }
                } catch (Exception e) {
                    // TODO
                } finally {
                    if (reader != null) {
                        try {
                            reader.close();
                        } catch (IOException e) {
                            // ignore
                        }
                    }
                }
            }
        }).start();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

The command should be executed inside a service:

public class Service extends IntentService {
...

    @Override
    protected void onHandleIntent(Intent intent) {
        String[] command = new String[]{"/a.out"};
        Class.execute(command);
    }

}

But I only get one line as an output, nothing more, but it should be hundreds of them. I already tried it on my computer by replacing EventBus.getDefault().post(new MessageEvent(line+"\n")); with System.out.println(line); and it works just fine.

Additional information: Subscriber handles EventBus through:

@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {
    tv.append(event.getMessage());
}

@Override
public void onStart() {
    super.onStart();
    EventBus.getDefault().register(this);
}

@Override
public void onStop() {
    EventBus.getDefault().unregister(this);
    super.onStop();
}

And tv is a TextView: TextView tv = (TextView)findViewById(R.id.TextView);

This structure was tested with a simple method, which in the following shown. It updated the TextView and there was more than one line passed to the TextView. So I guess it must be something inside my execution method.

public static void test() {
    for(int i = 0; i<500000; i++)
        EventBus.getDefault().post(new MessageEvent(i+"\n"));
}

Why does the EventBus only passes one line? What can I change to get the expected result?

Edit: Changing onMessageEvent() to:

public void onMessageEvent(MessageEvent event) {
    Log.i("Activity",event.getMessage());
}

Also just one line.

Tested it with another executable, now it works just fine, any idea why it works now?

1 Answers1

0

Is it passing only one line? It looks like your appending all the text into a single line by doing this -> tv.append(event.getMessage());

In your onMessageEvent() method try to System.out.println(event.getMessage()); to assert if the problem is really from EventBus.

Victor Gomes
  • 128
  • 10
  • Well sorry, I forgot to add what tv is, it's a TextView (I also added the information to the question). I don't think it's the EventBus because a dummy function (just a simple for-loop) was successful (more info in question). But I'll give it a try as soon as possible. –  Jul 12 '17 at 14:49
  • Tried it, but instead of System.out.println I used Log.i(), no input from "Activity" inside my logcat. –  Jul 13 '17 at 11:41