I am unable to retrieve the output of "db2 list db directory" command in my Java program. Basically, what I am trying to do is this:-
- A combo box is populated with db2 instances in the local system
- User selects a particular instance from the combo box
- A new Process is run to list the database for that instance
- Display the database as another combo box
This is the piece of code I have done:-
// dbinstances is a Combo box (Eclipse SWT widget)
this.dbInstances.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent arg0) {
// get selected instance name
String instance = dbInstances.getText();
// command invokes db2 command window, sets current instance and issues list db command
String command = "db2cmd /c \"set DB2INSTANCE="+instance+" & db2 list db directory\"";
// execute command and read output
try{
Process p = Runtime.getRuntime().exec(command);
BufferedReader br= new BufferedReader(new InputStreamReader(p.getInputStream()));
String op = null;
while((op=br.readLine())!=null){
System.out.println(op);
}
}
catch(IOException ioe){
ioe.printStackTrace();
}
}
public void widgetDefaultSelected(SelectionEvent arg0) {}
});
The problem is that the command executes, I am unable to retrieve the output. The window just opens and closes.
One solution I tried was to redirect the output to a temporary file and read it. It works, but is quite inefficient, since this piece of code runs each time the user selects an instance.
I am running DB2 9.7 Enterprise edition on Windows XP SP3 machine.
Any thoughts on how to retrieve the output in the Java program?
Thanks a lot in advance.
String command = "db2cmd /c /w /i \"set DB2INSTANCE="+instance+" & db2 list db directory\"";more info here: ibm.com/developerworks/data/library/techarticle/0307fierros/…