我有以下代码:
String[] command = {freeBcp,dbName + "." + dbUsername + "." + tableName,"in",file.getPath(),"-S" + dbServer,"-c","-t,","&"};
log.info("Invoking command: " + Arrays.toString(command));
try {
ProcessBuilder builder = new ProcessBuilder(command);
builder.redirectErrorStream(true);
Process process = builder.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
int exitValue = -1;
while ((line = br.readLine()) != null) {
log.info("BCP PROCESS: " + line);
}
log.debug("Close output stream..");
process.getOutputStream().close();
log.debug("Close error stream..");
process.getErrorStream().close();
log.debug("Close input stream..");
process.getInputStream().close();
log.debug("Waiting for freebcp Process to terminate..");
exitValue = process.waitFor();
if (exitValue == 0) {
log.debug("Exit value is 0..");
return true;
} else {
log.debug("Exit value is " + exitValue +"..");
return false;
}
} catch (IOException e) {
log.error("BCP PROCESS", e);
throw new BulkCopyException("Failed to bcp file (" + file.getPath() + ")", e);
}
catch (InterruptedException e) {
log.error("BCP PROCESS", e);
throw new BulkCopyException("Failed to bcp file (" + file.getPath() + ")", e);
}
它运行 freebcp 进程,打印出“Exit value is 0..”,因此该进程已完成,但它只是停止并且从不调用 return 语句。
如果我在 Putty 中运行命令,它会按预期运行。任何线索可能是什么问题?
这是我在日志中得到的:
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [INFO] BCP PROCESS:
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [INFO] BCP PROCESS: Starting copy...
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [INFO] BCP PROCESS: 1000 rows sent to SQL Server.
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [INFO] BCP PROCESS: 2000 rows sent to SQL Server.
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [INFO] BCP PROCESS: 3000 rows sent to SQL Server.
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [INFO] BCP PROCESS: 4000 rows sent to SQL Server.
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [INFO] BCP PROCESS: 5000 rows sent to SQL Server.
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [INFO] BCP PROCESS: 6000 rows sent to SQL Server.
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [INFO] BCP PROCESS: 7000 rows sent to SQL Server.
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [INFO] BCP PROCESS: 8000 rows sent to SQL Server.
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [INFO] BCP PROCESS: 9000 rows sent to SQL Server.
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [INFO] BCP PROCESS: 10000 rows sent to SQL Server.
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [INFO] BCP PROCESS: 11000 rows sent to SQL Server.
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [INFO] BCP PROCESS: 12000 rows sent to SQL Server.
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [INFO] BCP PROCESS: 13000 rows sent to SQL Server.
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [INFO] BCP PROCESS: 14000 rows sent to SQL Server.
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [INFO] BCP PROCESS: 15000 rows sent to SQL Server.
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [INFO] BCP PROCESS: 16000 rows sent to SQL Server.
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [INFO] BCP PROCESS: 17000 rows sent to SQL Server.
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [INFO] BCP PROCESS: 18000 rows sent to SQL Server.
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [INFO] BCP PROCESS: 19000 rows sent to SQL Server.
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [INFO] BCP PROCESS: 20000 rows sent to SQL Server.
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [INFO] BCP PROCESS: 21000 rows sent to SQL Server.
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [INFO] BCP PROCESS: 22000 rows sent to SQL Server.
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [INFO] BCP PROCESS: 23000 rows sent to SQL Server.
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [INFO] BCP PROCESS: 24000 rows sent to SQL Server.
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [INFO] BCP PROCESS: 25000 rows sent to SQL Server.
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [INFO] BCP PROCESS: 26000 rows sent to SQL Server.
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [INFO] BCP PROCESS: 27000 rows sent to SQL Server.
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [INFO] BCP PROCESS: 28000 rows sent to SQL Server.
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [INFO] BCP PROCESS: 29000 rows sent to SQL Server.
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [INFO] BCP PROCESS: 30000 rows sent to SQL Server.
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [INFO] BCP PROCESS: 31000 rows sent to SQL Server.
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [INFO] BCP PROCESS: 31660 rows copied.
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [DEBUG] Close output stream..
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [DEBUG] Close error stream..
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [DEBUG] Close input stream..
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [DEBUG] Waiting for freebcp Process to terminate..
2014-04-07 14:48:07 BulkCopy taskExecutor-1 [DEBUG] Exit value is 0..