Runtime.getRuntime().exec()对流重定向的命令无效怎么处理

Runtime.getRuntime().exec()对流重定向的命令无效怎么办?
Runtime.getRuntime().exec("ls > ~/1.txt") 
执行后,1.txt还是空的。

为了解决这个问题,有人想出了一个很轻巧的办法:

http://www.codefutures.com/weblog/andygrove/2008/06/generated-script-approach-to-running.html


        // 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(); 
    }