Tag Archives: Runtime.getRuntime().Exec()

How to use Runtime.getRuntime().Exec()

1. Wrong way to use it

Runtime.getRuntime().exec("xxx");

2. Correct usage

//You need to specify parameter one: command location; parameter two: -c means execute the first parameter first; parameter three: your command.
Runtime.getRuntime().exec(new String[]{"/bin/sh","-c","xxx"});

Such as:

try {
    Runtime.getRuntime().exec(new String[]{"/system/bin/sh","-c","reboot"});
}catch (Exception e) {
    e.printStackTrace();
}