Run Oracle import Command from Java and see Console output

时间秒杀一切 提交于 2019-12-25 08:18:02

问题


I am running an Oracle database Command for a .dmp file like this:

String impcmd = "imp askul/askul@askdb file=mydumpfile.dmp log=mylogfile.log fromuser=askul touser=askul full=N ignore=Y grants=Y indexes=Y";
Process p = Runtime.getRuntime().exec(impcmd);
p.waitFor();
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = br.readLine();
while(line != null){
System.out.println(line);
line = br.readLine();
}

The database import is Happening Fine on the Background, but I want to be Able to see the console output as the Import goes on as I now have to guess whether it is complete or not. What Am I missing here?


回答1:


You need to capture the stdout and stderr in separate threads (to prevent blocking) and output this as you get it, whilst waiting for the process to complete.

Note that you may need to read both stdout and stderr. Or your output may be going to the configured log file instead.

See this answer for more info and references to example code. Also check this article, which discusses common pitfalls when using Runtime.exec()




回答2:


I believe you just want to move the call to waitFor.

// p.waitFor();
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = br.readLine();
while(line != null){
  System.out.println(line);
  line = br.readLine();
}
p.waitFor(); // <-- to here.


来源:https://stackoverflow.com/questions/21433706/run-oracle-import-command-from-java-and-see-console-output

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!