例如说我执行了如下的命令
 Process proc = Runtime.getRuntime().exec("echo Hello");
现在我想获得命令行的输出,也就是说我要获得Hello这个字符串,我该怎么做?
好像Process有getOutputStream()的方法,但是直接toString()打印出来的是类名,我该怎么怎么获得Shell的输出呢

解决方案 »

  1.   

    OutputStream os = proc.getOutputStream();
    int c = 0;
    while((c = os.read()) != -1){
    System.out.println((char)c);
    }
      

  2.   

    太好了,谢谢你leeight(睡醒了!) 
    不过得InputStream is = proc.getInputStream();
                int c = 0;
                
                while((c = is.read()) != -1){
                System.out.print((char)c);
                }
    才能取得输出
      

  3.   

    但是还有个问题,您给的这个方法并不能获得所有的输出,例如我ps -Al|grep l2tpd|grep -v grep就眉宇把我相应的进程信息打印出来