亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

java Runtime如何執(zhí)行多條命令

 更新時(shí)間:2021年11月03日 09:15:38   作者:積極流年  
這篇文章主要介紹了java Runtime如何執(zhí)行多條命令,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

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("服務(wù)器不是linux|windows操作系統(tǒng)");
        }
    }

Runtime.getRuntime().exec 執(zhí)行多條

中間加上 & 或者 && 就可以執(zhí)行多條了.

Runtime.getRuntime().exec("cmd1 && " +
"cmd2 && " +
"cmd3 && " );

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論