Thread: pg_dump in PostgreSQL version 8.2 not working in Java app

pg_dump in PostgreSQL version 8.2 not working in Java app

From
"Michael Schmidt"
Date:
Folks,
I have an eclipse rcp application that includes backup capability by invoking pg_dump via Java ProcessBuilder.  This has been working fine with PostgreSQL 8.1 but quit working when I upgraded to 8.2.  Nothing else has changed.
 
The parameters I pass to the ProcessBuilder via a String Array in my test case are:

[C:\Program Files\PostgreSQL\8.2\bin\pg_dump.exe, --host=localhost, --port=5432, -Upostgres, -W, --verbose, --format=custom, --file=E:\Backup\PostgreSQL\test.bak, npbase]

I've concatenated these parameters into a string and run it from the Command Prompt it works fine except, of course, you have to put "" around C:\Program_Files\...\pg_dump.exe.

The following snippet creates and starts the ProcessBuilder:
    ProcessBuilder pb = new ProcessBuilder(cmdParms);
    mainProc = pb.start();
    // Put a buffered reader on stderr, where the PostgreSQL log is output
    procReader = new BufferedReader(new InputStreamReader(mainProc.getErrorStream()));
    // Set up a writer to output the password.
    procWriter = new PrintWriter(mainProc.getOutputStream());
 
I then call a method that waits for the password: prompt by reading from the procReader and appending a StringBuffer.  When password: is received, it sends the password.
    do {
        tempInt = procReader.read();
        tempSB.append((char) tempInt);
        if (-1 != tempSB.indexOf("Password: ")) {
            procWriter.println(pwString);
            procWriter.flush();
            break;
        }
    } while (procReader.ready());
 
With 8.2, this job now hangs.  I've spent several hours debugging and have found the following.  First, pb.start() does not throw an exception, so it appears to be finding pg_dump.exe and trying to start it.  It appears that procReader().lready() is always false and that the hang occurs in the do() loop. 
 
Again, all of this worked fine with 8.1.  Has something changed with pg_dump?
 
Michael Schmidt

Re: pg_dump in PostgreSQL version 8.2 not working in Java app

From
"Michael Schmidt"
Date:
Folks,
I've continued working on this problem and the plot thickens.  I'm not even able to get output from psql.exe through Java.  I ran the following simple test code:
 
// String[] parms = new String[] {"C:\\Windows\\System32\\ipconfig.exe", ""};
String[] parms = new String[]{"C:\\Program Files\\PostgreSQL\\8.2\\bin\\psql.exe","-Upostgres"};
File f = new File(parms[0]);
if (!f.isFile()) {
    System.out.println("Error - this isn't a file");
}
ProcessBuilder pb = new ProcessBuilder(parms);
pb.redirectErrorStream(true);
Process p = pb.start();
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
    System.out.println(line);
}
System.out.println("Exit with " + p.exitValue());
 
The ipconfig utility simply outputs the internet configuration.  I used the "" String to simulate a parameter.  This code identifies parm[0] as a valid file, outputs the configuration, and returns an exit code of 0, as it should.  When I switch the parms (comment out the ipconfig String[] line and remove the comments from the psql.exe String[] line), the process hangs.  So, with identical code, ipconfig works and psql hangs.  What the hey?  And again, this did not happen with 8.1.
 
Michael Schmidt