runtime.exec( new String[]{"/bin/bash", "-c", "java HelloWorld >> output.txt"} );
runtime.exec( new String[]{"/bin/bash", "-c", "java HelloWorld >> output.txt"} ,null );
runtime.exec( new String[]{"/bin/bash", "-c", "java HelloWorld >> output.txt"} ,null,null );
不过要注意,如果使用java /home/path/HelloWorld 时,' / '会被解析成 " . ",从而报出 “错误: 找不到或无法加载主类 .home.path.HelloWorld ”.
所以,无法使用全路径的时候,我们需要更改一下策略,把 路径 改到工作目录dir 中去,比如:
File dir = new File("/home/path/");
然后用其第6种重载方法,把dir作为第三个参数传入即可:
String []cmdarry ={"/bin/bash", "-c", "java HelloWorld >> output.txt"}
runtime.exec(cmdarry,null.dir);
当然echo , ls 等命令便不受' / '限制了。