我如何使用 ExpectJ 在 Java 中调用 pg_dump(在 PostgreSQL 8.4 上)?
我在很多地方都看到过这个问题,也就是说以编程方式从 PostgreSQL 进行备份的问题。我的解决方案是使用 ExpectJ,但我无法让我的代码正常工作。
我有以下代码:
public class BackupUtility
{
public static void main(String[] args)
{
try
{
ExpectJ exp = new ExpectJ(20);
Spawn shell = exp.spawn("\"C:\\Program Files\\PostgreSQL\\8.4\\bin\\pg_dump.exe\" " +
"-h localhost -U myUserName myDB");
shell.expect("Password: ");
shell.send("myPassword");
System.out.println(shell.getCurrentStandardOutContents());
shell.expectClose()
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
但是,在 shell.expect 行上,它超时了。
当我从命令提示符运行此命令时,它看起来如下所示:
C:\Documents and Settings\bobjonesthe3rd>"C:\Program Files\PostgreSQL\8.4\bin\pg_dump" -h localhost -U myUserName myDB
密码:
所以,我知道它会提示输入密码,但 ExpectJ 由于某种原因没有收到提示。
任何帮助将不胜感激。
谢谢, 马特
I've seen this problem many places, that is to say the problem with programmatically making backups from PostgreSQL. My solution is to use ExpectJ, but I'm having trouble getting my code to work.
I have the following code:
public class BackupUtility
{
public static void main(String[] args)
{
try
{
ExpectJ exp = new ExpectJ(20);
Spawn shell = exp.spawn("\"C:\\Program Files\\PostgreSQL\\8.4\\bin\\pg_dump.exe\" " +
"-h localhost -U myUserName myDB");
shell.expect("Password: ");
shell.send("myPassword");
System.out.println(shell.getCurrentStandardOutContents());
shell.expectClose()
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
However, on the shell.expect line, it's timing out.
When I run this command from the command prompt, it looks like the following:
C:\Documents and Settings\bobjonesthe3rd>"C:\Program Files\PostgreSQL\8.4\bin\pg_dump" -h localhost -U myUserName myDB
Password:
So, I know it prompts for a password, but ExpectJ isn't receiving the prompt for some reason.
Any help would be much appreciated.
Thanks,
Matt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我很确定一种更简单的方法,无需使用期望和发送,是通过 .pgpass 文件。也在 pg_dump 文档 中引用。
I'm pretty sure an easier way to do that, without the need to use expect and send, is via a .pgpass file. Also referenced in the pg_dump docs.