Runtime.getRuntime().exec to pass parameter when prompted Runtime.getRuntime().exec to pass parameter when prompted database database

Runtime.getRuntime().exec to pass parameter when prompted


You can open the OutputStream of the process and pass the passphrase which will be read by the server as if you typed it is coming from STDIN.

final Process startServer = Runtime.getRuntime().exec("PostgreQL -D data/dir/ start");PrintStream ps = new PrintStream(startServer.getOutputStream());ps.println(passPhrase);


One alternative to the answer provided above is to create a password file.

(e.g ~/.pgpass file (%APPDATA%\postgresql\pgpass.conf on Windows) formatted as:

hostname:port:database:username:password


Start by having a look at ProcessBuilder, it will make you life much easier....

ProcessBuilder pb = new ProcessBuilder("PostgreQL", "-D", "data/dir/" "start");Process p = pb.start();InputStream is = p.getInputStream();OutputStream os = p.getOutputStream();// Create a separate thread to read the input as needed...// Create a seperate thread to deal with the output as needed...