java调用bat,卡死在某一步

JAVA 代码:
   p = Runtime.getRuntime().exec("cmd /c E:\\TestAutomation\\TAReport\\a.bat");
   InputStream fis=p.getInputStream();
   InputStreamReader isr=new InputStreamReader(fis);
   BufferedReader br=new BufferedReader(isr);
   String line=null;
   while((line=br.readLine())!=null) {
                System.out.println(line);
            }

BAT代码:
@echo off
echo ******************************************
echo TAReport Start, wait for a moment please!!!
echo ******************************************
set CLASSPATH=%CLASSPATH%;%cd%\lib\classes12.jar;%cd%\lib\mail.jar;%cd%\lib\activation.jar;%cd%\TAReport.jar
java TaReport
echo Report has been sent, please check mail box!!!
exit

结果执行java程序的时候 只打印到echo ******************************************

执行 java TaReport 卡死了,单独直接双击BAT是可以的

最开始的需求是 以某种方式调用多个BAT,如果某个BAT 执行异常或者crash了 不影响其他BAT的执行,目前是想通过执行BAT,然后控制台输出的信息进行判断是否正常执行

还有其他方法不,如果用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"

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读