根据命令执行
/** * 判断什么操作系统 */ public String osName = System.getProperty("os.name"); /** * 根据命令执行, * @param cmdstr * @param isNeedReturn * @return list * @throws Exception */ public List<String> execute(String cmdstr, boolean isNeedReturn) throws Exception { //存储结果 List<String> lineList = new ArrayList<String>(); String[] cmdarray; if (osName.startsWith("Windows")) { cmdarray = new String[]{"cmd", "/c", cmdstr}; } else { cmdarray = new String[]{"/bin/bash", "-c", cmdstr}; } //执行命令 Process process = Runtime.getRuntime().exec(cmdarray); if (isNeedReturn) { //获取结果流 InputStream fis = process.getInputStream(); //读取结果流 BufferedReader br = new BufferedReader(new InputStreamReader(fis)); String line = null; while ((line = br.readLine()) != null) { if (line.trim().length() != 0) { lineList.add(line); } } return lineList; } return null; }
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。