3

我知道有很多关于从 java 执行进程的已解决问题。但是我无法使用提供的答案来解决我的问题。我正在尝试从 java 应用程序创建 postgresql 数据库备份。我使用以下代码

        //ProcessBuilder probuilder = new ProcessBuilder(new String[]{"cmd","/c","D:/PostgreSQL 8.2/bin/pg_dump.exe","-U","usr","-i","-h","localhost","-p","5432","-F","c","-b","-f","D:/backup test/backups/test_27-1-2013_210.backup", "test"});
        //ProcessBuilder probuilder = new ProcessBuilder(new String[]{"cmd","/c","D:\\PostgreSQL 8.2\\bin\\pg_dump.exe","-U","usr","-i","-h","localhost","-p","5432","-F","c","-b","-f","D:\\backup test\\backups\\test_27-1-2013_210.backup", "test"});
        ProcessBuilder probuilder = new ProcessBuilder(new String[]{"cmd","/c","\"D:\\PostgreSQL 8.2\\bin\\pg_dump.exe\"","-U","usr","-i","-h","localhost","-p","5432","-F","c","-b","-f","\"D:\\backup test\\backups\\test_27-1-2013_210.backup\"", "test"});
        Map<String, String> env = probuilder.environment();
        env.put("PGPASSWORD", "mypass");

        final Process process = probuilder.start();

执行上述代码后,出现以下错误: D:\PostgreSQL' is not recognized as an internal or external command, operable program or batch file.

仅当备份文件的路径包含空格时才会出现问题,否则会创建备份。我尝试在文件路径中同时使用斜杠和反斜杠,并引用了文件路径,但每次都会遇到相同的错误。可以从命令提示符执行命令。

我做错了什么。ProcessBuilder 中带有空格的参数数量是否存在一些限制。谢谢

4

1 回答 1

5

因为pg_dump.exe它是一个 exe(不是 .bat),所以你根本不需要cmd它,它可能导致的问题多于它解决的问题。只需exe直接调用,并删除文件路径周围的额外引号:

new String[]{"D:\\PostgreSQL 8.2\\bin\\pg_dump.exe","-U","usr","-i",
  "-h","localhost","-p","5432","-F","c","-b",
  "-f","D:\\backup test\\backups\\test_27-1-2013_210.backup", "test"}
于 2013-02-27T12:00:06.833 回答