的Java的ProcessBuilder与空间的多个PARAMS(Java ProcessBuild

2019-07-20 15:38发布

我知道有很多关于执行从java.But我无法使用提供的答案来解决我的问题解决过程中的问题。 我试图去创建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.

当路径备份文件包含空格,否则创建备份问题只occures。 我曾尝试同时使用斜线和反斜线在文件的路径和我引用文件的路径,但我每次都遇到了同样的错误。 命令可以从命令提示来执行。

我做错了。 有没有关于在的ProcessBuilder空格的参数个数一定的局限性。 谢谢

Answer 1:

由于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"}


文章来源: Java ProcessBuilder with multiple params with spaces