首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

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

2012-12-18 
Runtime.getRuntime().exec()对流重定向的命令无效怎么办?Runtime.getRuntime().exec(ls ~/1.txt)执行

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

热点排行