Java ProcessBuilder具有多个带空格的参数

我知道关于从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中带空格的参数数量是否有一些限制。谢谢

回答:

由于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具有多个带空格的参数 的全部内容, 来源链接: utcz.com/qa/415726.html

回到顶部