Runtime.getRuntime().exec(cmd)😭“ls -alh >> output.txt”);
当有重定向时,是不能执行的
这时候看一看官方的文档:
public Process exec(String command) throws IOExecption
public Process exec(String command,String [] envp) throws IOExecption
public Process exec(String command,String [] envp,File dir) throws IOExecption
public Process exec(String[] cmdarray) throws IOExecption
public Process exec(String[] cmdarray,String [] envp) throws IOExecption
public Process exec(String[] cmdarray,String [] envp,File dir) throws IOExecption
重载方法。使用第二种:
String[] cmd = {"/bin/bash","-c","nohup sleep 3 >> 1.txt &"};
Process process = Runtime.getRuntime().exec(cmd);