当前位置:首页 > 通信资讯 > 正文

java多线程执行方法(java同时执行多个方法)

目录
  • java Runtime如何执行多条命令
  • Runtime.getRuntime().exec 执行多条

java Runtime如何执行多条命令

使用 && 分隔命令

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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(); //获得系统属性集 String osName = props.getProperty("os.name"); //操作系统名称 if(osName.toLowerCase().contains("linux")){ return "/bin/sh -c"; }else if(osName.toLowerCase().contains("windows")){ return "cmd /c"; }else{ throw new RuntimeException("服务器不是linux|windows操作系统"); } }

Runtime.getRuntime().exec 执行多条

中间加上 & 或者 && 就可以执行多条了.

?
1 2 3 Runtime.getRuntime().exec("cmd1 && " + "cmd2 && " + "cmd3 && " );

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/lanlianhua_luffy/article/details/106694469

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。