Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to execute a cmd line when prompts for password with java

Tags:

java

cmd

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..

like image 985
Constantin Onofrei Avatar asked Oct 09 '22 19:10

Constantin Onofrei


1 Answers

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

like image 90
Constantin Onofrei Avatar answered Oct 13 '22 10:10

Constantin Onofrei