我用java进程运行一个外部命令。该命令是带有一些 db2 命令的 .bat 文件。当我想使用进程 waiffor() 返回结果时,我什么也得不到。程序必须是阻塞的。
爪哇代码:
.....
Runtime rt = Runtime.getRuntime();
Process p = null;
String command = "db2cmd -c -w -i C:/import1.bat";
p = rt.exec(command);
p.waitFor();
.....
进口1.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
我也
p.getInputStream()
用来处理InputStream
,但该过程总是被第三个命令阻止(db2 import.....)
代码:
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;
}
当我使用db2 export...
或db2 select * from ....
替换第三个命令时。该函数waitfor
可以返回结果。进程不能被阻塞。
这太奇怪了。