1

I have an application that has a directory on the SD card. The application saves notes in a new subdirectory. I want to delete the whole subdirectory using shell command "rm -r" but the application throws an exception:

04-02 23:14:23.410: W/System.err(14891): java.io.IOException: Error running exec(). Command: [cd, /mnt/sdcard/mynote, &&, rm, -r, aaa] Working Directory: null Environment: null

Can anyone help me?

user3132352
  • 403
  • 1
  • 9
  • 24
  • 4
    You really should do this using java file operations, rather than executing a process - which, as you are finding out, is tricky and non-portable. But if you really want help with the current attempt, you will need to post your code. For one thing, it looks like you are trying to issue a sequence of commands. Instead of trying to change to the directory in question, specify it as an argument to `rm`. – Chris Stratton Apr 02 '14 at 20:24
  • possible duplicate of [Delete a folder on SD card](http://stackoverflow.com/questions/5701586/delete-a-folder-on-sd-card) – Robby Pond Apr 02 '14 at 20:31

1 Answers1

6

This happens because you used Runtime.exec(String). Never use this function. It's hard to predict and only works in trivial cases. Always use Runtime.exec(String[]).

Since cd and && are not commands but shell features, you need to manually invoke a shell for them to work:

Runtime.getRuntime().exec(new String[] {
    "sh", "-c", "cd /mnt/sdcard/mynote && rm -r aaa"  
});

On a related note, you should never pass String data unescaped to shells. For example, this is wrong:

// Insecure, buggy and wrong!
String target = "aaa";
Runtime.getRuntime().exec(new String[] {
    "sh", "-c", "cd /mnt/sdcard/mynote && rm -r " + target  
});

The correct way is to pass data as separate parameters to the shell, and reference them from your command:

// Secure and correct
String target = "aaa";
Runtime.getRuntime().exec(new String[] {
    "sh", "-c", "cd /mnt/sdcard/mynote && rm -r \"$1\"", "--", target
});

For example, if a file is named * or My file, the incorrect version will delete a whole bunch of completely unrelated files. The correct version does not.

that other guy
  • 116,971
  • 11
  • 170
  • 194