开发者

process.exec not returning correct code

I have a Java program with code:

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 executes another program which has the folowing code:

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);
                                } 

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

But from the Test1, the exit value is always 0. HOw come, when the batch is executed, it will run the ConnectionTest class and it will get exception as it will not find DB2Driver22222.

Can anybody explain to me why I am not getting correct error code nor any error messages.


The problem is that you are recieving the return code of the start command, and not what the start command executes. Though start may see test.bat exit with code 1, start itself exits success (0). Execute the .bat directly instead:

String cmd = "test.bat";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜