2010-08-13 127 views
0

我有一个Java程序与代码:process.exec没有返回正确的代码

public class Test1 { 
public static void main(String args[]) throws InterruptedException, 
     IOException { 
    String cmd = "cmd /c start test.bat"; 
    Process p = Runtime.getRuntime().exec(cmd); 
    InputStream stderr = process.getErrorStream(); 
    InputStreamReader isr = new InputStreamReader(stderr); 
    BufferedReader br = new BufferedReader(isr); 
    String line = null; 

    while ((line = br.readLine()) != null){ 
     System.out.println(line);} 

     p.waitFor(); 
     int exitVal = p.exitValue(); 
     System.out.println(exitVal); 

}}

test.bat的执行其它程序具有如下因素代码:

public class ConnectionTest { 

public Connection getConn throws SQLException{ 
     Connection conn = null; 
     Statement st = null; 
     ResultSet rs = null; 
     String driverName = "com.ibm.db2.jcc.DB2Driver22222"; 
     try { 
       Class.forName(driverName).newInstance(); 
      } catch (Exception e) { 
       e.printStackTrace(); 

       System.exit(1); 
           } 

;;;; ;;;; ;;; ;;;

但是从Test1开始,退出值始终为0.当进入批处理时,它将运行 ConnectionTest类,它将得到异常,因为它不会找到DB2Driver22222。

有人可以向我解释为什么我没有得到正确的错误代码或任何错误消息。

回答

3

问题是,您正在接收start命令的返回代码,而不是启动命令的执行代码。虽然start可能会看到test.bat退出代码1,start本身退出成功(0)。直接执行.bat代替:

String cmd = "test.bat"; 
+0

非常感谢... – user234194 2010-08-13 18:02:05