java调用bat,卡死在某一步

来源:互联网 发布:淘宝大学宝贝发布流程 编辑:程序博客网 时间:2024/05/17 02:49
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 offecho ******************************************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.jarjava TaReportecho 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: TaReportCaused 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"

原创粉丝点击