2010-08-13 143 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