java exec()运行一个db2 import命令,waitfor()从不返回 [英] java exec() run a db2 import command, waitfor() never return

查看:337
本文介绍了java exec()运行一个db2 import命令,waitfor()从不返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用java进程运行一个外部命令。
这个命令是带有一些db2命令的.bat文件。
当我想使用进程waiffor()返回一个结果,我什么都不能得到。程序必须阻止。



java代码:

  ... 

运行时rt = Runtime.getRuntime();
进程p = null;

String command =db2cmd -c -w -i C:/import1.bat;

p = rt.exec(command);
p.waitFor();

.....

import1.bat:

  @echo off 
db2连接到text_DB用户文本使用tesxt0114

db2 IMPORT FROMC:\\ 方法P(1,2,3,4,5,6,7,8,9,10,11,12,,13,14,15,16)的消息C:\\\\ MVCMSInputFiles\IIS20121224180129.csv \\MVCMSInputFiles\20121224180129.log INSERT INTO BLUEX.BIZ_MACHINE(MACHINE_SSERIALNO,MACHINE_STYPE,MACHINE_SPROJECTID,MACHINE_SCATEGORY,MACHINE_SNAME,MACHINE_SBRAND,MACHINE_SSPOT,MACHINE_SPRODUCEDATE,MACHINE_SSERVPERIOD,MACHINE_SENDDATE,MACHINE_SCONTRACTID,MACHINE_SSERVSTATUS,MACHINE_NSTATUS,MACHINE_SSCID,LOG_SLASTUSER,MACHINE_TSIMPORTTIME)

db2 connect reset



我也使用 p.getInputStream()来处理 InputStream ,但是这个进程总是被第三个命令阻止(db2导入.....)



javacode:

 code> Runtime rt = Runti me.getRuntime(); 
进程p = null;

String command =db2cmd -c -w -i C:/import1.bat;

p = rt.exec(command);

BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

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

reader.close();

while(true){
if(p.waitFor()== 0)break;
}






当我使用 db2 export ... db2 select * from .... 替换第三个命令。函数 waitfor 可以返回结果。该进程无法阻止。



这太奇怪了。

解决方案

我得到了关于这个的答案题。
的原因是:
在p = rt.exec(command)之前,有没有提交的连接对象。
,所以这样的代码可以正常运行。

  .... 
ConnectionFactory。提交(conn);
p = rt.exec(command);
... ....


I used java process run a external command. This command is .bat file with some db2 commands. When I want use the process waiffor() return a result, I can't get anything. The program must be block.

java code:

.....

Runtime rt = Runtime.getRuntime();
Process p = null;     

String command = "db2cmd -c -w -i C:/import1.bat";

p = rt.exec(command);   
p.waitFor();

.....

import1.bat:

@echo off
db2 connect to text_DB user text using tesxt0114  

db2 IMPORT FROM "C:\MVCMSInputFiles\IIS20121224180129.csv" OF DEL METHOD P (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16) MESSAGES "C:\MVCMSInputFiles\20121224180129.log" INSERT INTO BLUEX.BIZ_MACHINE (MACHINE_SSERIALNO, MACHINE_STYPE, MACHINE_SPROJECTID, MACHINE_SCATEGORY, MACHINE_SNAME, MACHINE_SBRAND, MACHINE_SSPOT, MACHINE_SPRODUCEDATE, MACHINE_SSERVPERIOD, MACHINE_SENDDATE, MACHINE_SCONTRACTID, MACHINE_SSERVSTATUS, MACHINE_NSTATUS, MACHINE_SSCID, LOG_SLASTUSER, MACHINE_TSIMPORTTIME) 

db2 connect reset


I also use p.getInputStream() to handle InputStream, but the process always be block by the 3rd command (db2 import.....)

javacode:

Runtime rt = Runtime.getRuntime();
Process p = null;

String command = "db2cmd -c -w -i C:/import1.bat";

p = rt.exec(command);

BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

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

reader.close();

while(true){                
    if (p.waitFor() == 0) break;            
}


When I used db2 export... or db2 select * from .... replace the 3rd command. The function waitfor can return a result. The process can't be block.

That's too weird.

解决方案

I got the answer about this question. reason is : Before the p = rt.exec(command), there is connection object that not commit. so the code like this can run correctly.

.... ...
ConnectionFactory.commit(conn);
p = rt.exec(command); 
... ....

这篇关于java exec()运行一个db2 import命令,waitfor()从不返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆