首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >带有空格和双引号的Java - ProcessBuilder命令参数失败。

带有空格和双引号的Java - ProcessBuilder命令参数失败。
EN

Stack Overflow用户
提问于 2015-08-31 15:22:11
回答 1查看 7.3K关注 0票数 4

我正在使用ProcessBuilder运行一个Windows executable...the确切的命令,我需要运行的是:

代码语言:javascript
复制
"C:\Program Files\CCBU\CCBU.exe" -d"C:\My Data\projects\ccbu\ciccb-report.xls" -tf"C:\Program Files\CCBU\loss-billing-filters.txt"

如果我从命令提示符中运行上面的命令,它就能正常工作。

如果然后以字符串[]数组的形式发出命令和参数,如下面的StackOverflow post (ProcessBuilder adds extra quotes to command line)所示,则会失败,因为目录路径中的空格会将参数以某种方式中断到CCBU.exe可执行文件:

代码语言:javascript
复制
[log-snippet]
2015-08-31 10:39:08,937 [main] INFO  rpd.primary - C:\Program Files\CCBU\CCBU.exe
logging to the given report's directory
Configuration file is: ./CCBUConfigFile.txt
Running with the following settings:
Report Filepath:       C:\My
Search Terms FilePath: C:\Program

2015-08-31 10:39:08,948 [main] INFO  rpd.primary - STDERR:--------------------
2015-08-31 10:39:08,961 [main] INFO  rpd.primary - 
Warning: parameter Data\projects\ccbu\ciccb-report.xls not recognized. Ignoring

Warning: parameter Files\CCBU\loss-billing-filters.txt not recognized. Ignoring

Error: C:\Program not found or not readable
[/log-snippet]

如果我将数据文件和过滤器移动到没有空格的目录路径中,则工作正常:

代码语言:javascript
复制
"C:\Program Files\CCBU\CCBU.exe" -d"C:\Users\n0002501\ccbu\ciccb-report.xls" -tf"C:\Users\n0002501\ccbu\loss-billing-filters.txt" 

问题是,此过程的用户将把文件放在有空格的文件夹(目录)中。所以我得让它和空间一起工作。我想这很简单,但我错过了什么?

我使用本文中的类来处理STDOUT和STDERR:http://alvinalexander.com/java/java-exec-processbuilder-process-2的线程

下面是代码:

代码语言:javascript
复制
            // Split the Arguments : 
            // In Eclipse and runtime, the arguments get broken : 
            // The STDOUT from the command shows the Report Filepath
            // and Search Teams FilePath as broken at the 1st space...
            // 
            // Report Filepath:       C:\My
            // Search Terms FilePath: C:\Program
            // 
            // SHOULD BE : 
            // 
            // Report Filepath:       C:\My Data\projects\ccbu\ciccb-report.xls
            // Search Terms FilePath: C:\Program Files\CCBU\loss-billing-filters.txt
            // 
            try { 
                commands.add ( "\"C:\\Program Files\\CCBU\\CCBU.exe\""                      );
                commands.add ( "-d\"C:\\My Data\\projects\\ccbu\\ciccb-report.xls\""        );
                commands.add ( "-tf\"C:\\Program Files\\CCBU\\loss-billing-filters.txt\""   );
                commandExecutor = new SystemCommandExecutor(commands);
                commandExecutor.setLog ( getLog() );

                // DEBUG : Build and printout the commands...
                // 
                lstrCommand = "";
                for ( int theIdx=0; theIdx<commands.size (); theIdx++ ) {
                    if ( theIdx == 0 ) { 
                        lstrCommand = lstrCommand + commands.get ( theIdx );
                    }
                    else { 
                        lstrCommand = lstrCommand + " " + commands.get ( theIdx );
                    }
                    getLog().debug ( SHORT_NAME + " Building Command[] [" + commands.get ( theIdx ) + "]" );
                }

                getLog().debug ( SHORT_NAME + " Running Command[] [" + lstrCommand + "]" );

                result = commandExecutor.executeCommand();

                // get the stdout and stderr from the command that was run
                stdout = commandExecutor.getStandardOutputFromCommand();
                stderr = commandExecutor.getStandardErrorFromCommand();

                // print the stdout and stderr
                getLog().info ( "SystemCommandExecutor - Status Code [" + result + "]" );
                getLog().info ( "STDOUT:--------------------" );
                getLog().info( stdout );
                getLog().info ( "STDERR:--------------------" );
                getLog().info( stderr );
            }
            catch ( Exception ltheXcp ) { 
                getLog().error ( SHORT_NAME + ".runTask () - Error/exception on commands [3-spaces] [" + lstrCommand + "]" );
            }
            finally { 
                commands.clear ();
                stdout = null;
                stderr = null;
                commandExecutor = null;
            }

Jayan,工作的最终代码:

代码语言:javascript
复制
            try { 
                commands.add ( "C:\\Program Files\\CCBU\\CCBU.exe"                      );
                commands.add ( "-dC:\\My Data\\projects\\ccbu\\ciccb-report.xls"        );
                commands.add ( "-tfC:\\Program Files\\CCBU\\loss-billing-filters.txt"   );

                commandExecutor = new SystemCommandExecutor ( commands );
                commandExecutor.setLog ( getLog() );

我所要做的就是取出所有双引号,让ProcessBuilder自己处理目录路径.

蒂亚角

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-31 15:29:18

添加没有“双引号”的单个字符串。

代码语言:javascript
复制
                commands.add ( "C:\\Program Files\\CCBU\\CCBU.exe"                      );
                commands.add ( "-d");
                commands.add ("C:\\My Data\\projects\\ccbu\\ciccb-report.xls"        );
                commands.add ( "-tf");
                commands.add("C:\\Program Files\\CCBU\\loss-billing-filters.txt"   );
                commandExecutor = new SystemCommandExecutor(commands);

ProcessBuilder将负责对args的必要处理。

拉出评论:

贾扬,你的想法让我思考:以下几点奏效了: commands.add ( "-dC:\My Data\projects\ccbu\ciccb-report.xls“);commands.add ( "-tfC:\Program Files\CCBU\loss-tfC:\Program-filters.txt”) - lincolnadym

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32314645

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档