微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Runtime.exec不能运行“su – postgres -c'pg_dump …'”

这是我想运行的命令:

su - postgres -c "pg_dump ....."

备份postgres数据库

如果我现在在Linux shell,作为一个根,它的作品很好。

但是现在,我想从一个Java应用程序运行它,如下所示:

Java ImageIO.read()返回null

LWJGL抓取鼠标 – debugging应用程序是否挂起或断点与抓取的鼠标碰撞

运行时提升Java应用程序

通过使用eclipse运行.sh文件来debugging应用程序

用于Windows VHD API的Java库

String cmd = "su - postgres -c "pg_dump --port 5432 ....."" Process p = Runtime.getRuntime().exec(cmd); // read the error stream and input stream p.waitFor();

它会抛出一个错误

su: unkNown option "--port" please try "su --help" to get more @R_814_4045@ion

现在我改变代码为:

Process p = Runtime.getRuntime().exec(new String[0]{cmd}); // read the error stream and input stream p.waitFor(); Cannot run program "su - postgres -c "pg_dump ....."": java.io.IOException: error=2,no that file or directory

我现在应该怎么做?

如何使用Java在Linux中获得总磁盘空间?

来自Hell的幻影文件夹(旧Vista安装)导致Java String.equals()问题

等到tomcat完成启动

用于Windows上的SVN Java绑定(JavaHL)的配对JAR和本机库二进制文件

自定义urischeme与Java独立应用程序相关联

除了上面给出的答案(并且很好理解你的问题),请记住,只要把你的命令放在一个shell脚本文件中,你就可以让你的生活更轻松。 例如,一个只包含两行的dumppg.sh文件

#!/bin/sh su - postgres -c "pg_dump ....."

只是让它可执行,并(测试后)从Java调用它。

exec(new String[]{"sh","-c","su - postgres ..."});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐