java 调用bat

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Test {
 public static void main(String[] args) throws IOException {
  Process p = Runtime.getRuntime().exec("cmd /c D://a.bat");
  InputStream fis = p.getInputStream();
  final BufferedReader br2 = new BufferedReader(new InputStreamReader(p.getErrorStream()));
  InputStreamReader isr = new InputStreamReader(fis);
  final BufferedReader br = new BufferedReader(isr);
  Thread t1 = new Thread(){
   public void run(){
   String line = null;
   try {
    while ((line = br2.readLine()) != null) {
     System.out.println(line);
    }
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   }
  };
  Thread t2 = new Thread(){
   public void run(){
   String line = null;
   try {
    while ((line = br.readLine()) != null) {
     System.out.println(line);
    }
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   }
  };
  t1.start();
  t2.start();
 }
}


这样写就没问题了,API里有写:Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.因为本地进程输入输出缓存有限,找不到TaReport会导致stderr输出,你不快点读取的话Process就挂在那了。

下面是输出 :

******************************************
TAReport Start, wait for a moment please!!!
******************************************
java.lang.NoClassDefFoundError: TaReport
Caused by: java.lang.ClassNotFoundException: TaReport
 at java.net.URLClassLoader$1.run(Unknown Source)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.net.URLClassLoader.findClass(Unknown Source)
 at java.lang.ClassLoader.loadClass(Unknown Source)
 at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
 at java.lang.ClassLoader.loadClass(Unknown Source)
 at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: TaReport.  Program will exit.
Report has been sent, please check mail box!!!
Exception in thread "main"

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值