windows - Java - ProcessBuilder command arguments with spaces and double-quotes fails -
i'm using processbuilder run windows executable...the exact command need run :
"c:\program files\ccbu\ccbu.exe" -d"c:\my data\projects\ccbu\ciccb-report.xls" -tf"c:\program files\ccbu\loss-billing-filters.txt" if run above command command prompt, works fine.
if issue command , arguments indicated in following stackoverflow post (processbuilder adds quotes command line) string [] array fails, spaces in directory paths break arguments somehow ccbu.exe executable :
[log-snippet] 2015-08-31 10:39:08,937 [main] info rpd.primary - c:\program files\ccbu\ccbu.exe logging given report's directory configuration file is: ./ccbuconfigfile.txt running 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] if move data files , filters directory path no spaces works fine :
"c:\program files\ccbu\ccbu.exe" -d"c:\users\n0002501\ccbu\ciccb-report.xls" -tf"c:\users\n0002501\ccbu\loss-billing-filters.txt" the issue is, users of process placing files in folders (directories) have spaces. somehow have working spaces. i'm thinking it's simple, missing?
i'm using classes posting handle threads stdout , stderr : http://alvinalexander.com/java/java-exec-processbuilder-process-2
here's code :
// split arguments : // in eclipse , runtime, arguments broken : // stdout command shows report filepath // , search teams filepath broken @ 1st space... // // report filepath: c:\my // search terms filepath: c:\program // // should : // // 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 , printout commands... // lstrcommand = ""; ( 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(); // stdout , stderr command run stdout = commandexecutor.getstandardoutputfromcommand(); stderr = commandexecutor.getstandarderrorfromcommand(); // print stdout , 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 + "]" ); } { commands.clear (); stdout = null; stderr = null; commandexecutor = null; } jayan, final code 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 had take out double-quotes , let processbuilder handle directory paths on it's own...
tia, adym
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 take care of necessary handling of args.
pull comment:
jayan, you're idea got me thinking : following worked :
commands.add ( "-dc:\\my data\\projects\\ccbu\\ciccb-report.xls" ); commands.add ( "-tfc:\\program files\\ccbu\\loss-billing-filters.txt"); – lincolnadym
Comments
Post a Comment