日期:2014-05-20 浏览次数:20850 次
public static void rumCommand(String cmd) { Runtime runtime = Runtime.getRuntime(); int exitValue = 0; try { StringBuffer output = new StringBuffer(); StringBuffer err = new StringBuffer(); //logger.info("----runtime.exec start!----"); String[] cmdArray = { "/bin/sh", "-c", cmd }; logger.info("start"); Process proc = runtime.exec(cmdArray); logger.info("end"); //logger.info("----runtime.exec end!----"); /* * 系统需要通过提取某个命令的标准输出,来进行某项处理。例如,搜索/路径下的xml文件并对结果处理,如下: find / -name * "*.xml" -type f | xargs java 相应程序 * * 如果按照常规的方式,将以上的几个字串直接传递给Runtime.exec的参数,通过CommandExec的实现方法如下: * CommandExec exec = new CommandExec(); Process process = * exec.exec(new String(){"find", "/", "-name", "\"*.xml\"", * "-type", "f", "|", "xargs", "java", "相应程序"}); */ Command sgStd = new Command(proc.getInputStream(),"STDOUT",output); Command sgErr = new Command(proc.getErrorStream(),"ERROUT",err); sgStd.start(); sgErr.start(); exitValue = proc.waitFor(); //logger.info("----rumCommand exitValue: " + exitValue); } catch (Exception e) { //e.printStackTrace(); logger.info(e); runtime.exit(1); } }
String cmd1 = new String("cmd".getBytes("ISO-8859-1"),"UTF-8"); String cmd2 = new String("cmd".getBytes("ISO-8859-1"),"UTF-8"); String ss[] = {cmd1,cmd2};