import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class TestShell { public static void main(String[] args) throws IOException { try { String commands = "echo $PATH >> 1.txt"; Process process = Runtime.getRuntime().exec(new String[] { "sh", "-c", commands }); // for showing the info on screen InputStreamReader ir = new InputStreamReader(process.getInputStream()); BufferedReader input = new BufferedReader(ir); String line; while ((line = input.readLine()) != null) { System.out.println(line); } } catch (java.io.IOException e) { System.err.println("IOException " + e.getMessage()); } } }
参考 http://wangbaoaiboy.blog.163.com/blog/static/52111910201111892938552/
java使用Runtime.exec执行linux命令重定向或管道问题: http://my.oschina.net/hetiangui/blog/133981