二话不说,直接上代码!
package cn.sigangjun.util;
/**
* <p>Title:java call linux shell util </p>
* <p>Description: java call linux shell util</p>
* @since 2013-8-12 下午3:50:15
* @version 1.0
* @author <a style='color:red' href='http://blog.csdn.net/sigangjun'>sigangjun</a>
*/
public class JavaShellUtil {
public static int executeShell(String shellCommand) {
int success = 0;
try {
String[] cmd = {"/bin/sh", "-c", shellCommand};
Process pid = Runtime.getRuntime().exec(cmd);
if (pid == null) {
success = 1;
}
} catch (Exception e) {
e.printStackTrace();
success = 1;
}
return success;
}
}