JAVA如何调用exe或者批处理文件

  1. /** 
  2.  * JavaExec.java version 1.0    Feb 22, 2010  
  3.  */  
  4.    
  5.  import java.lang.Runtime;  
  6.  import java.lang.Process;  
  7.  import java.io.InputStreamReader;  
  8.  import java.lang.Exception;  
  9.  import java.io.*;  
  10. public class JavaExec {  
  11.      /** 
  12.      * @param args 
  13.      * @author Eric Yang  
  14.      * @ 
  15.      */  
  16.     public static void main(String[] args) {  
  17.         // TODO Auto-generated method stub   
  18.              
  19.         JavaExec javaExec = new  JavaExec();  
  20.         javaExec.testProcess();  
  21.     }  
  22.       
  23.     public void testProcess()    
  24.     {    
  25.         try    
  26.         {    
  27.           
  28.         String home="c:/process";    
  29.         String command = "bfimport.bat -U yq -P qq -x bfimport_71.xml  -p";    
  30.         String[] cmd = new String[] { "cmd.exe"" ""bfimport.bat" };     
  31.         File dir = new File(home);    
  32.         //Process process = Runtime.getRuntime().exec(command,null, dir);     
  33.         Process process = Runtime.getRuntime().exec(command);    
  34.           
  35.         /*TestInputStream errorStream = new TestInputStream(process 
  36.             .getErrorStream()); 
  37.         errorStream.start();*/  
  38.         InputStream  inputStream = process.getInputStream();  
  39.         BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));     
  40.           
  41.         String line = null;    
  42.         while ((line = reader.readLine()) != null) {    
  43.                     System.out.println(line);    
  44.         }   
  45.          //int exitValue = process.waitFor();     
  46.          //System.out.println("Return Value:" + exitValue);     
  47.          process.getOutputStream().close();   
  48.         }    
  49.         catch(IOException e)    
  50.         {    
  51.             System.out.println("IOException ");  

 

 

 

修改其中的command就可以了

上面的例子确实可以获得一些输出,但是有很多情况也无法获取,再参考很多实例之后,下面的例子最强大,几乎可以捕获所有类型的命令行输出。

 

[java:nogutter] view plain copy print ?
  1. /** 
  2.  * JavaExec.java version 1.0    Feb 22, 2010  
  3.  */  
  4.    
  5.  import java.lang.Runtime;  
  6.  import java.lang.Process;  
  7.  import java.io.InputStreamReader;  
  8.  import java.lang.Exception;  
  9.  import java.io.*;  
  10.    
  11.    
  12.  class StreamDrainer implements Runnable {    
  13.      private InputStream ins;    
  14.      
  15.      public StreamDrainer(InputStream ins) {    
  16.          this.ins = ins;    
  17.      }    
  18.      
  19.      public void run() {    
  20.          try {    
  21.              BufferedReader reader = new BufferedReader(    
  22.                      new InputStreamReader(ins));    
  23.              String line = null;    
  24.              while ((line = reader.readLine()) != null) {    
  25.                  System.out.println(line);    
  26.              }    
  27.          } catch (Exception e) {    
  28.              e.printStackTrace();    
  29.          }    
  30.            
  31.      }    
  32.      
  33.  }     
  34.    
  35. public class JavaExec {  
  36.     /** 
  37.      * @param args 
  38.      * 
  39.      *  
  40.      * @author Eric Yang  
  41.      * @ 
  42.      */  
  43.     public static void main(String[] args) {  
  44.         // TODO Auto-generated method stub   
  45.        System.out.println("/n Usage:java JavaExec command or  java JavaExec");  
  46.          
  47.         JavaExec javaExec = new  JavaExec();  
  48.         if (args.length >= 1) {  
  49.             javaExec.testProcess(args[0]);  
  50.         }  
  51.         else{  
  52.              javaExec.testProcess(null);          
  53.         }     
  54.           
  55.     }  
  56.       
  57.     public void testProcess(String userInputCmd)    
  58.     {    
  59.         try    
  60.         {    
  61.           
  62.         String home="C:/jas_utilities";    
  63.         String command = "bfimport.bat -U root -P root -x C:/etc/bfimport_71.xml  -p";     
  64.         String[] cmd = new String[] { "bfimport.bat",   
  65.                             "-U ","root ",  "-P"," root ""-x ""C:/fvt/7120modify/src/bfimport_71.xml"" -p" };     
  66.         File dir = new File(home);    
  67.         //Process process = Runtime.getRuntime().exec(command,null, dir);     
  68.         Process process = null;  
  69.         if(userInputCmd == null){  
  70.             process = Runtime.getRuntime().exec(command,null, dir);    
  71.             System.out.println("default command");  
  72.         }  
  73.         else{  
  74.             process = Runtime.getRuntime().exec(userInputCmd);   
  75.             System.out.println("user input command");  
  76.         }  
  77.           
  78.         System.out.println("-----------------output informaiton------");   
  79.         new Thread(new StreamDrainer(process.getInputStream())).start();    
  80.          System.out.println("-----------------output informaiton------/n");  
  81.            
  82.         System.out.println("-----------------error informaiton------");                
  83.         new Thread(new StreamDrainer(process.getErrorStream())).start();  
  84.         System.out.println("-----------------error informaiton------");               
  85.         process.getOutputStream().close();    
  86.           
  87.               
  88.         
  89.        int exitValue = process.waitFor();    
  90.        System.out.println("Return Value:" + exitValue);    
  91.        process.destroy();  
  92.       
  93.         }    
  94.         catch(Exception e)    
  95.         {    
  96.             System.out.println("Exception:"+e.getMessage() );    
  97.         }    
  98.    }   
  99.      
  100. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值