Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
836 views
in Technique[技术] by (71.8m points)

windows - Java - ProcessBuilder command arguments with spaces and double-quotes fails

I'm using ProcessBuilder to run a Windows executable...the exact command I need to run is :

"C:Program FilesCCBUCCBU.exe" -d"C:My Dataprojectsccbuciccb-report.xls" -tf"C:Program FilesCCBUloss-billing-filters.txt"

If I run the above command from a command prompt, it works fine.

If I then issue the command and arguments as indicated in the following StackOverflow post (ProcessBuilder adds extra quotes to command line) as a String [] array it fails, as the spaces in the directory paths break the arguments somehow to the CCBU.exe executable :

[log-snippet]
2015-08-31 10:39:08,937 [main] INFO  rpd.primary - C:Program FilesCCBUCCBU.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 Dataprojectsccbuciccb-report.xls not recognized. Ignoring

Warning: parameter FilesCCBUloss-billing-filters.txt not recognized. Ignoring

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

If I move the data files and the filters to a directory path with no spaces this works fine :

"C:Program FilesCCBUCCBU.exe" -d"C:Users
0002501ccbuciccb-report.xls" -tf"C:Users
0002501ccbuloss-billing-filters.txt" 

The issue is, the users of this process will be placing files in folders (directories) that DO have spaces. So somehow I have to get it working with spaces. I'm thinking it's something simple, but what am I missing?

I'm using the Classes from this posting to handle the Threads for STDOUT and STDERR : http://alvinalexander.com/java/java-exec-processbuilder-process-2

Here's the code :

            // 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 Dataprojectsccbuciccb-report.xls
            // Search Terms FilePath: C:Program FilesCCBUloss-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, The final code that works :

            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() );

All I had to do is take out all the double-quotes and let ProcessBuilder handle the directory paths on it's own...

tia, adym

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Add individual strings without "double" quotes..

                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 will take care of necessary handling of args.


Pull up comment:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...