Runtime.getRuntime().exec()对流重定向的命令无效怎么办?
Runtime.getRuntime().exec("ls > ~/1.txt") 执行后,1.txt还是空的。 // generate a script file containg the command to run final File scriptFile = new File("/tmp/runcommand.sh"); PrintWriter w = new PrintWriter(scriptFile); w.println( "#!/bin/sh" ); w.println( cmd ); w.close(); // make the script executable Process p = Runtime.getRuntime().exec( "chmod +x " + scriptFile.getAbsolutePath() ); p.waitFor(); // execute the script p = Runtime.getRuntime().exec( scriptFile.getAbsolutePath() ); p.waitFor(); }