process.exec 未返回正确的代码

发布于 2024-09-14 17:53:56 字数 1264 浏览 10 评论 0原文

我有一个 Java 程序,代码如下:

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。

谁能向我解释为什么我没有收到正确的错误代码或任何错误消息。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

于我来说 2024-09-21 17:53:56

问题是您收到的是 start 命令的返回代码,而不是收到 start 命令执行的内容。尽管 start 可能会看到 test.bat 退出并显示代码 1,但 start 本身会成功退出 (0)。直接执行 .bat:

String cmd = "test.bat";

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";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文