Java ProcessBuilder,带有多个带有空格的参数 [英] Java ProcessBuilder with multiple params with spaces

查看:731
本文介绍了Java ProcessBuilder,带有多个带有空格的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多解决问题有关执行过程从java.But我无法使用提供的答案解决我的问题。
我试图从java应用程序创建postgresql数据库备份。我使用以下代码

I know that there is a lot of solved questions regarding executing processes from java.But I am unable to solve my problem using answers provided. I am trying go create postgresql database backup from java application. I use following code

        //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'不被识别为内部或外部命令,
可操作的程序或批处理文件。

After executing above code i get following error: D:\PostgreSQL' is not recognized as an internal or external command, operable program or batch file.

备份文件路径包含空格,否则创建备份。
我试图在文件路径中使用斜线和反斜杠,我引用文件路径,但我每次都得到相同的错误。可以从命令提示符执行命令。

Problem occures only when path to backup file contains spaces otherwise backup is created. I have tried to use both slash and backslash in the file path and I quoted file path but i get the same error every time. Command can be executed from command prompt.

我做错了。在ProcessBuilder中有空格的参数数量有一些限制。
感谢

What I am doing wrong. Is there some limitations regarding number of parameters with spaces in ProcessBuilder. Thanks

推荐答案

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

Since pg_dump.exe is an exe (not a .bat) you don't need the cmd at all, and it is probably causing more problems than it solves. Just call the exe directly, and remove the extra set of quotes around the file paths:

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,带有多个带有空格的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆