Java tutorial
/* * Copyright 2011-2016 ZXC.com All right reserved. This software is the confidential and proprietary information of * ZXC.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into with ZXC.com. */ package com.ms.commons.standalone.utils; import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import com.ms.commons.log.LoggerFactoryWrapper; /** * <pre> * java?shell???? * ???cat2G * java??shell * ???shell??????? * </pre> * * @author zxc Apr 12, 2013 8:58:38 PM */ public class Shell { private static final Logger logger = LoggerFactoryWrapper.getLogger(Shell.class); public static String exec(String cmd) { Process process = null; String[] cmds = { "/bin/bash", "-c", cmd, }; try { process = new ProcessBuilder(cmds).redirectErrorStream(true).start(); byte[] buffer = IOUtils.toByteArray(process.getInputStream()); process.waitFor(); return new String(buffer, "utf-8"); } catch (Exception e) { logger.error("runtime.exec cmd: " + cmd + " failed", e); } finally { if (process != null) { process.destroy(); } } return ""; } public static void main(String[] argv) { System.out.println(exec("ls /home")); } }