/**
* 判斷什么操作系統(tǒng)
*/
public String osName = System.getProperty("os.name");
/**
* 根據(jù)命令執(zhí)行,
* @param cmdstr
* @param isNeedReturn
* @return list
* @throws Exception
*/
public List execute(String cmdstr, boolean isNeedReturn) throws Exception {
//存儲(chǔ)結(jié)果
List lineList = new ArrayList();
String[] cmdarray;
if (osName.startsWith("Windows")) {
cmdarray = new String[]{"cmd", "/c", cmdstr};
} else {
cmdarray = new String[]{"/bin/bash", "-c", cmdstr};
}
//執(zhí)行命令
Process process = Runtime.getRuntime().exec(cmdarray);
if (isNeedReturn) {
//獲取結(jié)果流
InputStream fis = process.getInputStream();
//讀取結(jié)果流
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;
}
標(biāo)題名稱:根據(jù)命令執(zhí)行
新聞來源:
http://weahome.cn/article/jsjdpp.html