Java - 使用 ProcessBuilder 执行带有空格和双引号的命令参数失败

5
我正在使用ProcessBuilder来运行一个Windows可执行文件...我需要运行的确切命令是:
"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帖子(ProcessBuilder添加额外的引号到命令行)所示将命令和参数作为String []数组发出,由于目录路径中的空格会以某种方式破坏CCBU.exe可执行文件的参数,因此它会失败:
[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]

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

"C:\Program Files\CCBU\CCBU.exe" -d"C:\Users\n0002501\ccbu\ciccb-report.xls" -tf"C:\Users\n0002501\ccbu\loss-billing-filters.txt" 

问题是,这个过程的用户将文件放在带有空格的文件夹中。所以我必须想办法让它能够处理空格。我认为这是一件简单的事情,但我错过了什么?
我正在使用此帖子中的类来处理标准输出和标准错误流的线程:http://alvinalexander.com/java/java-exec-processbuilder-process-2 下面是代码:
            // 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,最终可运行的代码:

            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自行处理目录路径...

谢谢,adym


没有空格的标志和参数看起来很奇怪。 - Caffeinated
是的,而且 CCBU 实用程序不允许我在那里放置空格...它是一种由 *.exe 前端支持的 Python 实用程序。 - lincolnadym
1个回答

5

在不带“双引号”的情况下添加个别字符串。

                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会处理参数的必要操作。


上移注释:

Jayan, You're idea got me thinking : The following worked :

 commands.add ( "-dC:\\My Data\\projects\\ccbu\\ciccb-report.xls" );
 commands.add ( "-tfC:\\Program Files\\CCBU\\loss-billing-filters.txt"

); – lincolnadym


嗨Jayan,你是正确的,这样可以正确处理空格,但问题是ProcessBuilder现在会在“-d”和“C:\ My Data \ projects \ ccbu \ ciccb-report.xls”之间以及“-tf”和“C:\ Program Files \ CCBU \ loss-billing-filters.txt”之间放置一个空格,导致CCBU实用程序出错:警告:未识别参数C:\ My Data \ projects \ ccbu \ ciccb-report.xls。忽略警告:未识别参数C:\ Program Files \ CCBU \ loss-billing-filters.txt。忽略 - lincolnadym
这是一个奇怪的工具...看起来-tf"C:\Users\n0002501\ccbu\loss-billing-filters.txt"必须是单个字符串:尝试使用commands.add("-tf"C:\Users\n0002501\ccbu\loss-billing-filters.txt")。 - Jayan
1
Jayan,你的想法让我思考了一下: 以下内容可行: commands.add("-dC:\My Data\projects\ccbu\ciccb-report.xls"); commands.add("-tfC:\Program Files\CCBU\loss-billing-filters.txt"); - lincolnadym

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接