简体   繁体   English

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

[英]Invoke postgres commands via Java Runtime.getruntime.exec()

Initially I ran this code without the leading "cmd" and I received an access denied message. 最初,我运行此代码时没有前导"cmd"并且收到访问拒绝消息。 Postgres is being run as a service by an account that cannot be logged into and I am an administrator running this application. Postgres由无法登录的帐户作为服务运行,我是运行此应用程序的管理员。 The attempt with "cmd" included returns me no input. 包含"cmd"的尝试未返回任何输入。 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. 生成的异常是Postgres_Bin目录上的“ Access Denied异常。 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

First if you want to run something in context of windows shell you have to use cmd /c . 首先,如果要在Windows Shell上下文中运行某些程序,则必须使用cmd /c Otherwise you are just running cmd itself. 否则,您将只运行cmd本身。 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. 修复代码后,与未使用cmd时获得的访问权限完全相同,这是因为您似乎没有足够的权限。 To solve the problem 1. fix your permissions 2. if it is impossible try to run external process as different user using runas command. 解决问题的方法1.修复您的权限2.如果不可能,请尝试使用runas命令以其他用户身份运行外部进程。

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. 从该错误看来,您正在尝试运行目录,而不是附加“ psql”或“ pg_dump”来获取实际的可执行文件名称。

I would also believe you wouldn't need to include the "cmd" on the beginning, so try taking that out again. 我也相信您不需要在一开始就包含“ cmd”,因此请尝试再次删除它。

java.io.IOException: Cannot run program "d:\\program files\\postgres\\bin" java.io.IOException:无法运行程序“ 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) 此错误表明路径中的空格使exec()调用混乱(并且目录bin确实不是Windows可以运行的任何内容)

Make sure you enclose the full path to the .exe with double quotes because of the spaces. 由于空格,请确保用双引号将.exe的完整路径括起来。

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. 我认为与使用exec()相比,建议使用ProcessBuilder,因为您可以指定参数和程序以不同的参数运行,从而避免使用空格分隔路径名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM