通过 Java Runtime.getruntime.exec() 调用 postgres 命令

发布于 2024-10-07 02:06:07 字数 1443 浏览 1 评论 0原文

最初,我在没有前导的 "cmd" 的情况下运行了此代码,并且收到了一条访问被拒绝的消息。 Postgres 正在作为一项服务由无法登录的帐户运行,而我是运行该应用程序的管理员。包含 "cmd" 的尝试不会返回任何输入。我的问题是如何执行这些语句以实现结果文件和数据更改?

 String[] psqlCommands = {"cmd ",postgresLocation, " -dDatabase ", " -UUser ",
                "-c ",  "UPDATE host.user SET service = 1 WHERE service = 1;" +
                        "UPDATE host.permission SET service = 1 WHERE service = 2"};
       Runtime.getRuntime().exec(psqlCommands);

        String[] pgDumpCommands = {"cmd ", postgresLocation, " --data-only ",
                "-t host.user_info -t host.permission -t host.group -t host.account -t host.password " +
                        "-UUser Database> "
                        + DATA + "\\dataExport.sql"};

        Runtime.getRuntime().exec(pgDumpCommands);

生成的异常是 Postgres_Bin 目录中的 Access Denied 异常。一旦获得必要的材料,我会发布痕迹。

线程“main”中出现异常 java.io.IOException:无法运行程序“d:\program f” iles\postgres\bin": CreateProcess 错误=5,访问被拒绝
在 java.lang.ProcessBuilder.start(来源未知) 在 java.lang.Runtime.exec(来源未知)
在 java.lang.Runtime.exec(来源未知)
在 Thing.main(Thing.java:85)
引起原因:java.io.IOException:CreateProcess error=5,访问被拒绝
在 java.lang.ProcessImpl.create(本机方法)
在 java.lang.ProcessImpl.(来源未知)
在 java.lang.ProcessImpl.start(来源未知)
... 4 更多

Initially I ran this code without the leading "cmd" and I received an access denied message. Postgres is being run as a service by an account that cannot be logged into and I am an administrator running this application. The attempt with "cmd" included returns me no input. My question is how do I go about executing these statements to achieve the resulting files and data changes?

 String[] psqlCommands = {"cmd ",postgresLocation, " -dDatabase ", " -UUser ",
                "-c ",  "UPDATE host.user SET service = 1 WHERE service = 1;" +
                        "UPDATE host.permission SET service = 1 WHERE service = 2"};
       Runtime.getRuntime().exec(psqlCommands);

        String[] pgDumpCommands = {"cmd ", postgresLocation, " --data-only ",
                "-t host.user_info -t host.permission -t host.group -t host.account -t host.password " +
                        "-UUser Database> "
                        + DATA + "\\dataExport.sql"};

        Runtime.getRuntime().exec(pgDumpCommands);

The exception that is generated is an Access Denied exception that is on the Postgres_Bin directory. I will post a trace once I get the necessary materials.

Exception in thread "main" java.io.IOException: Cannot run program "d:\program f
iles\postgres\bin": CreateProcess error=5, Access is denied
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at Thing.main(Thing.java:85)
Caused by: java.io.IOException: CreateProcess error=5, Access is denied
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 4 more

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千里故人稀 2024-10-14 02:06:07

从错误来看,您似乎正在尝试运行该目录,而不是附加“psql”或“pg_dump”来获取实际的可执行文件名称。

我还相信您不需要在开头包含“cmd”,因此请尝试再次将其删除。

From the error it looks like you're trying to run the directory, and not appending "psql" or "pg_dump" to get the actual executable name.

I would also believe you wouldn't need to include the "cmd" on the beginning, so try taking that out again.

神仙妹妹 2024-10-14 02:06:07

java.io.IOException:无法运行程序“d:\program files\postgres\bin”

此错误表明路径中的空格混淆了 exec() 调用(并且目录 bin 确实什么都没有Windows 可以运行)

请确保将 .exe 的完整路径用双引号引起来,因为其中有空格。

我认为建议使用 ProcessBuilder 而不是使用 exec(),因为您可以指定参数和程序以使用不同的参数运行,从而避免使用空格转义路径名。

java.io.IOException: Cannot run program "d:\program files\postgres\bin"

This error indicates that the spaces in the path confuse the exec() call (and the directory bin is indeed nothing that Windows can run)

Make sure you enclose the full path to the .exe with double quotes because of the spaces.

I think using ProcessBuilder is recommended over using exec() as you can specify the arguments and the program to run with different parameters avoiding the escaping of path names with spaces.

能否归途做我良人 2024-10-14 02:06:07

首先,如果您想在 Windows shell 上下文中运行某些内容,则必须使用 cmd /c。否则你只是运行 cmd 本身。这就是你什么也得不到的原因。

当修复你的代码时,你应该会被拒绝访问,就像你没有使用 cmd 时一样,因为你似乎没有足够的权限。解决问题
1.修复你的权限
2. 如果不可能,请尝试使用 runas 命令以不同用户身份运行外部进程。

First if you want to run something in context of windows shell you have to use cmd /c. Otherwise you are just running cmd itself. This is the reason that you do not get anything.

When fix your code you are expected to get access denied exactly as you got when you did not use cmd because it seems that you do not have enough permissions. To solve the problem
1. fix your permissions
2. if it is impossible try to run external process as different user using runas command.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文