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

编程入门 行业动态 更新时间:2024-10-28 21:21:21
本文介绍了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,带有多个带有空格的参数

本文发布于:2023-11-25 16:43:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630537.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   空格   参数   Java   ProcessBuilder

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!