编程语言
首页 > 编程语言> > java – Runtime.getRuntime().exec在提示时传递参数

java – Runtime.getRuntime().exec在提示时传递参数

作者:互联网

我使用下面的java语句从java代码启动PostgreSQL服务器.我使用Runtime.getRuntime().exec()方法来实现这一点.

 final Process startServer = Runtime.getRuntime().exec("PostgreQL -D data/dir/ start");

这里当服务器启用SSL并且密钥受密码保护时,在命令行中它会提示输入密码(参见下面的行).在这种情况下,我如何传递密码.

Is server running?
starting server anyway
waiting for server to start......Enter PEM pass phrase:....

在提示时,我们是否有任何选项可以将密码作为参数传递?或者在开始时在PostgreSQL中我们有任何传递keypassword的规定(比如 – “PostgreSQL -d data / dir / -keyPass password start”)?

请帮帮我.任何帮助或建议将非常感激.提前致谢.

解决方法:

您可以打开进程的OutputStream并传递将由服务器读取的密码,就像您键入的密码来自STDIN一样.

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

标签:runtime-exec,java,postgresql,process,database
来源: https://codeday.me/bug/20190901/1782120.html