java调用Linux命令报错:java.io.IOException: Cannot run program "ps": CreateProcess error=2, ?????????

时间:2024-01-19 13:52:56
在idea里面,java代码:Runtime.getRuntime().exec("ps -aux")

java调用Linux命令报错:java.io.IOException: Cannot run program "ps": CreateProcess error=2, ?????????

是因为默认是用windows平台运行了,所以报错,得改成调用Linux平台运行:
String[] cmd = new String[3];
cmd[0] = "C:/Program Files (x86)/Git/bin/sh.exe" ;
cmd[1] = "-c" ;
cmd[2] ="D:/shell.sh";
Process proc = Runtime.getRuntime().exec(cmd); cmd[0]是Linux平台的地址,cmd[1]固定,cmd[2]是运行的shell命令