2

I can't seem to get runtime.exec working in my android app. I've tried it with a host of shell utilities, here's the code I'm using:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    filesPrinter = (Button) findViewById(R.id.print_files);
    filesPrinter.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try {
                Process proc = Runtime.getRuntime().exec("ls");
                out = new BufferedWriter(new OutputStreamWriter(proc.getOutputStream()));
                in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
                String line;
                while((line = in.readLine()) != null) {
                    System.out.println(line);
                }
                System.out.println("Done reading");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
 }

I don't get an error, but also do not get anything in logcat.

MEURSAULT
  • 8,307
  • 4
  • 24
  • 23

3 Answers3

2

The problem ended up being a bug with the eclipse logcat. Using adb logcat, I could see everything that was supposed to be outputted. For some reason, logcat on eclipse showed that it was connected but was not receiving any application level output from the emulator.

MEURSAULT
  • 8,307
  • 4
  • 24
  • 23
  • Come to think of it, I think LogCat does not display data from the standard error stream (which is where `e.printStackTrace()` outputs). Try `System.out.println(Arrays.toString(e.getStackTrace()));` instead, or, better yet, any of the `Log` methods. – mikołak Jun 22 '12 at 22:58
0

Maybe your current working directory (which is what ls scans without any parameters) simply contains no files. Try providing a path as a command argument.

mikołak
  • 9,605
  • 1
  • 48
  • 70
  • I'm assuming that by "nothing" you mean "nothing except >>Done reading<<", correct me if I'm wrong. – mikołak Jun 22 '12 at 21:23
0

Think you're missing a proc.waitFor()....

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    filesPrinter = (Button) findViewById(R.id.print_files);
    filesPrinter.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try {
                Process proc = Runtime.getRuntime().exec("ls");
                out = new BufferedWriter(new OutputStreamWriter(proc.getOutputStream()));
                in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
                String line;
                while((line = in.readLine()) != null) {
                    System.out.println(line);
                }
                System.out.println("Done reading");
                //
                proc.waitFor(); // THIS!!!
                //
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
 }
t0mm13b
  • 34,087
  • 8
  • 78
  • 110