MEURSAULT
MEURSAULT

Reputation: 8819

Can't get runtime.exec working on android

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.

Upvotes: 2

Views: 4395

Answers (3)

t0mm13b
t0mm13b

Reputation: 34592

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();
            }
        }
    });
 }

Upvotes: 0

MEURSAULT
MEURSAULT

Reputation: 8819

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.

Upvotes: 2

mikołak
mikołak

Reputation: 9705

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.

Upvotes: 0

Related Questions