Java代码执行shell命令
public static void runShellCommand(String command) {
String[] cmdStrings = new String[] {"sh", "-c", command};
String retString = "";
Process p = null;
try {
p = Runtime.getRuntime().exec(cmdStrings);
int status = p.waitFor();
if (status != 0) {
System.err.println(String.format("runShellCommand: %s, status: %s", command,
status));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (p != null) {
p.destroy();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}