WHCSRL 技术网

java 使用Runtime.getRuntime().exec(command)执行命令和脚本

1.执行脚本

(1) linux和mac
String cmd = "sh "+path+ File.separator+"sh/containisenable.sh redis";
String[] command = {"/bin/sh", "-c", cmd};
System.out.println(cmd);
Process exec = Runtime.getRuntime().exec(command);
(2)Windows
String[] command = { "cmd.exe", "/c", cmmd };
Process ps = Runtime.getRuntime().exec(command);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

2.执行命令

String cmd = "cp /home/test.json /opt";
String[] cmds = new String[]{"sh","-c",cmd};
exec = Runtime.getRuntime().exec(cmds);
  • 1
  • 2
  • 3
推荐阅读