Hi all,
I'm trying to execute a string of command using Runtime.exec. This
string may contain space or other meta-charcters which has been
escaped. Does anyone know any tools out there does the parsing and
possibly put them into an array or in a collection for me?
i.e. on the extreme side, here is a possible string (assuming user
always uses double slashes)
"\"C:\\My Test\\my test batch.bat" argA \"arg B\" \"arg\ C\" \"arg\tD\"
\"arg \/E\""
This may or may not be an everyday usecase, but it does happens.
Does anyone out there know how java.exe parse the command line string
to a string array which then used by "static void main(String[] args)"?
Thank you guys!
Jimmy
Robert Mark Bram - 31 Oct 2006 06:14 GMT
Hi Jimmy,
> i.e. on the extreme side, here is a possible string (assuming user
> always uses double slashes)
> "\"C:\\My Test\\my test batch.bat" argA \"arg B\" \"arg\ C\" \"arg\tD\"
> \"arg \/E\""
When you call Runtime.exec(), you don't need to have all the arguments
quoted.
Instead, use one of the forms of exec() that accepts a string array -
cmdarray - as the first argument. For example:
exec(String[] cmdarray)
The first string is the command and each string after that is a command
line argument. Of course, this assumes you have control over how the
input to your code gets formatted, so it might not help you...
Rob
:)
Paul Hamaker - 31 Oct 2006 09:09 GMT
Just a note: Better not use spaces in dir/filenames.
jimmy_please@yahoo.com - 31 Oct 2006 17:26 GMT
Thanks for opions guys. Unfortunately, I have no control on whether if
the directory contains space or not. So double-quotes need to be used
some how. I can write a little parser to do that but I thought if
there's open-source tool does that and it will be great.
Tools like CLI from Apache is expected a pre-parse string array to feed
in ... is there a library out there will take the string as it is for
parsing? ... and just through ParseException of some sort to identicate
there's a format problem?
Robert Mark Bram - 01 Nov 2006 00:07 GMT
Hi Jimmy,
> Thanks for opions guys. Unfortunately, I have no control on whether if
> the directory contains space or not. So double-quotes need to be used
[quoted text clipped - 4 lines]
> parsing? ... and just through ParseException of some sort to identicate
> there's a format problem?
I don't know about an external API, but the exec method I mentioned
handles the quoting of arguments for you - try it out and you will find
that if you have "arg two" in the 3rd position of the arg array, it
will come out as being a whole second arg, not arg two and three.
Rob
:)