/**
* 判断什么操作系统
*/
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;
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。