白色水泥:java调用shell,并得到shell的输出

来源:百度文库 编辑:偶看新闻 时间:2024/04/29 20:36:46

java调用shell,并得到shell的输出

    博客分类:
  • linux/shell/sed/awk
Java Java代码  
  1. import java.io.IOException;  
  2. import java.io.InputStreamReader;  
  3. import java.io.LineNumberReader;  
  4.   
  5. public class RunShell {  
  6.     public static void main(String[] args) {  
  7.         try {  
  8.             ///opt/flow_video/flow_total.sh 2011-05-26 11:08:02 12:08:07 192.168.1.152,192.168.1.153  
  9.             Process process = Runtime.getRuntime().exec("/opt/flow_video/tf.sh 2011-05-23 11:08:02 12:08:07");  
  10.             InputStreamReader ir = new InputStreamReader(process.getInputStream());  
  11.             LineNumberReader input = new LineNumberReader(ir);  
  12.             String line;  
  13.             while ((line = input.readLine()) != null)  
  14.             System.out.println(line);  
  15.             input.close();  
  16.             ir.close();  
  17.         } catch (IOException e) {  
  18.             e.printStackTrace();  
  19.         }     
  20.     }  
  21.   
  22. }