6

Hy,

I try to dump a postgresql database from my java application.

In cmd it goes ok:

pg_dump -U user database>outfile.sql 

and CMD prompts for password

To access this command line from my java:

Runtime.getRuntime().exec("pg_dump -U user database>outfile.sql"); 

and now it waits for password.

I tried with Runtime.getRuntime().exec(String[])... but nothing.

Somehow, I must set the parameter 'password' in .exec() method,

pg_dump statement does not accept password in it's command line..

Ken White
  • 123,280
  • 14
  • 225
  • 444

4 Answers4

2

thanks Max,

I tried this

    Process p = Runtime.getRuntime().exec(cmd);
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
    bw.write("password"); 
    //bw.eriteLine();
    bw.flush();

but still nothing. I used InputStreamReader to see errors or messages from cmd execution, and nothig. It still waits for 'Password'.

But I had another solution :

    Process p = Runtime.getRuntime().exec("cmd /c start cmd.exe /C\"pg_dump -U user -W db>outfile.sql\"");

so the black prompter opens with 'Password:'. write the password, hit enter and it goes off.

that's enough for me.

Thanks

1

Missed to press "Enter". This modification did the trick for me:

bw.write("password" + "\n");
enoliking
  • 31
  • 1
  • 1
1

Full working solution:

Process process = Runtime.getRuntime().exec(cmd);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
bw.write("password\n"); 
bw.flush();

Tried to update @cortexiphan solution, but my edit was rejected.

kfox
  • 1,176
  • 13
  • 16
0

You could redirect the standard input of the process as described in this question.

Essentially, they make use of the Process.getInputStream method and write to the stream this method returns (e.g. with a BufferedWriter).

You get the neccessary Process object returned by the exec() method.

Community
  • 1
  • 1
Mira Weller
  • 2,406
  • 22
  • 27