【JAVA代码】java执行shell命令
public class ExecShell { public static final Logger logger = LoggerFactory.getLogger(ExecShell.class); public static Object exec(String cmd) { try { String[] cmdA = {"/bin/sh", "-c", cmd}; Process process = Runtime.getRuntime().exec(cmdA); LineNumberReader br = new LineNumberReader(new InputStreamReader( process.getInputStream())); StringBuffer sb = new StringBuffer(); String line; int i = 0; while ((line = br.readLine()) != null) { if (i > 0) { sb.append("\n"); } sb.append(line); i++; } logger.info("cmd:" + cmd + ",result:" + sb.toString()); return sb.toString(); } catch (Exception e) { logger.error("exec error", e); } return null; } }