process.exec 未返回正确的代码
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您收到的是
start
命令的返回代码,而不是收到 start 命令执行的内容。尽管start
可能会看到test.bat
退出并显示代码 1,但start
本身会成功退出 (0)。直接执行 .bat:The problem is that you are recieving the return code of the
start
command, and not what the start command executes. Thoughstart
may seetest.bat
exit with code 1,start
itself exits success (0). Execute the .bat directly instead: