通过 Java Runtime.getruntime.exec() 调用 postgres 命令
最初,我在没有前导的 "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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从错误来看,您似乎正在尝试运行该目录,而不是附加“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.
此错误表明路径中的空格混淆了 exec() 调用(并且目录 bin 确实什么都没有Windows 可以运行)
请确保将 .exe 的完整路径用双引号引起来,因为其中有空格。
我认为建议使用 ProcessBuilder 而不是使用 exec(),因为您可以指定参数和程序以使用不同的参数运行,从而避免使用空格转义路径名。
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.
首先,如果您想在 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.