簡體   English   中英

Java - 帶有空格和雙引號的 ProcessBuilder 命令參數失敗

[英]Java - ProcessBuilder command arguments with spaces and double-quotes fails

我正在使用 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" 

問題是,這個過程的用戶將把文件放在有空格的文件夾(目錄)中。 所以不知何故,我必須讓它與空間一起工作。 我在想這很簡單,但我錯過了什么?

我正在使用此帖子中的類來處理 STDOUT 和 STDERR 的線程: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 自己處理目錄路徑......

蒂亞,阿迪姆

添加不帶“雙”引號的單個字符串..

                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 進行必要的處理。


拉起評論:

Jayan,你的想法讓我思考:以下工作:

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

); – 林肯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM