Reputation: 1031
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:-
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.
Upvotes: 0
Views: 2538
Reputation: 1031
Ok, figured this one out. The required solution is to add /w and /i switches to the command :-
// command invokes db2 command window, sets current instance and issues list db command
String command = "db2cmd /c /w /i \"set DB2INSTANCE="+instance+" & db2 list db directory\"";
According to IBM developerWorks
// Additional information about db2cmd Options
-c Execute the DB2 command window and terminate.
-w Wait until the DB2 command window terminates.
-i Inherit the environment from the invoking shell.
-t Inherit the title from the invoking shell
Upvotes: 1
Reputation: 7693
You can also use the DB2 API via JNI in order to retrieve the database list directory. You have to start the scan, get the entries, and then close the scan.
By doing this, you can control the db list in a better way that parsing an output that could variate by many reasons (HADR, the authentication mechanism, local or remote, with or without alias, ip address or server name, service name or port number, in linux (home dir) or in Windows (drive letter), and other things) The DB2 API is the same in all platforms, so it is almost platform independent, you just have to know which library load (.so or .dll), but the rest is the same.
For more information take a look at: db2DbDirOpenScan http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.db2.luw.apdv.api.doc/doc/r0001509.html db2DbDirGetNextEntry http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.db2.luw.apdv.api.doc/doc/r0001492.html db2DbDirCloseScan http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.db2.luw.apdv.api.doc/doc/r0001437.html
Upvotes: 1