日期:2014-05-20  浏览次数:20626 次

java执行cmd命令的基础问题
用Runtime.getRuntime().exec()   方法执行dos命令
怎么得到返回值?    

还有   我想执行多个dos命令应该怎么搞啊?

比如先执行cd\
再执行echo   111> 1.txt     是连着执行
而不是写两次Runtime.getRuntime().exec()


谢谢。。

------解决方案--------------------
1)写个bat
2)cmd /c dir / && echo 111> 1.txt
------解决方案--------------------
import java.io.*;
public class dir
{
public static void main(String args[])
{
try{
Process pr = Runtime.getRuntime().exec( "cmd.exe /c java A ");
StringBuffer sbOut = new StringBuffer(1000);
BufferedReader br = new BufferedReader(new InputStreamReader(pr.getInputStream()));
while (true) {
String s = br.readLine();
if (s==null) break;
System.out.println(s);
}
br.close();
pr.waitFor();
System.out.println (sbOut.toString());
if(pr.exitValue()==0)
{
System.out.println ( "运行成功!! ");
}
} catch (Exception e) {
System.out.println ( "Unexpected error executing cmd: " + e);
}
}
}
希望能帮到你...