Run Oracle import Command from Java and see Console output

1.7k views Asked by At

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?

2

There are 2 answers

2
Brian Agnew On BEST ANSWER

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()

4
Elliott Frisch On

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.