java command line space & escape character parser for Runtime.exec

J

jimmy_please

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
 
R

Robert Mark Bram

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
:)
 
J

jimmy_please

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?
 
R

Robert Mark Bram

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
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?

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
:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,235
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top