java Runtime如何執(zhí)行多條命令
更新時間:2021年11月03日 09:15:38 作者:積極流年
這篇文章主要介紹了java Runtime如何執(zhí)行多條命令,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
java Runtime如何執(zhí)行多條命令
使用 && 分隔命令
public static void cmd() {
String ls = " cd /home/ && dir ";
Process process = null;
String cmd = getOsCmd()+ ls;
try {
process = Runtime.getRuntime().exec(cmd);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(new String(line.getBytes(),"GBK"));
}
}catch (Exception e){
e.printStackTrace();
}
finally {
process.destroy();
}
}
public static String getOsCmd(){
Properties props=System.getProperties(); //獲得系統(tǒng)屬性集
String osName = props.getProperty("os.name"); //操作系統(tǒng)名稱
if(osName.toLowerCase().contains("linux")){
return "/bin/sh -c";
}else if(osName.toLowerCase().contains("windows")){
return "cmd /c";
}else{
throw new RuntimeException("服務器不是linux|windows操作系統(tǒng)");
}
}
Runtime.getRuntime().exec 執(zhí)行多條
中間加上 & 或者 && 就可以執(zhí)行多條了.
Runtime.getRuntime().exec("cmd1 && " +
"cmd2 && " +
"cmd3 && " );
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
微信js sdk invalid signature簽名錯誤問題的解決方法分析
這篇文章主要介紹了微信js sdk invalid signature簽名錯誤問題的解決方法,結合實例形式分析了微信簽名錯誤問題相關解決方法,需要的朋友可以參考下2019-04-04
springboot 使用yml配置文件自定義屬性的操作代碼
在SpringBoot中yml/yaml文件可以自定義一些屬性,以供注入給自定義bean對象的屬性,主要通過空格和層次來實現(xiàn),類似于python代碼,本文通過實例代碼給大家介紹springboot 使用yml配置文件自定義屬性,感興趣的朋友跟隨小編一起看看吧2024-03-03
詳解MyBatis中Executor執(zhí)行SQL語句的過程
MyBatis中獲取SqlSession時會創(chuàng)建執(zhí)行器Executor并存放在SqlSession中,本篇文章將以MapperMethod的execute() 方法作為起點,對MyBatis中的一次實際執(zhí)行請求進行說明,并結合源碼對執(zhí)行器Executor的原理進行闡釋2023-07-07

