command line arguments and quote marks

I need to send a block of text as a single command line argument to a java program.

In particular, I want the

String[] args

param of my java class's main to contain just a single element.

I am running JDK 1.5.0_10 on windoze xp, and I fully realize that the behavior I describe below is almost certainly due to bizarreness in the msdos shell, and has nothing to do with java, but I am submitting it here in the hopes that someone knowledgeable has seen this before and can help.

To provide a concrete illustration of the behavior I see, consider this toy class that merely echos its command line args:

publicclass CommandLineEcho{

publicstaticvoid main(String[] args){

for (int i = 0; i < args.length; i++){

System.out.println("args[" + i +"] = " + args[i]);

}

}

}

This simple excecution

java CommandLineEcho a b c

args[0] = a

args[1] = b

args[2] = c

behaves as expected.

Now suppose you want to get a single command line argument out of text like the above which has spaces in it.

In msdos, you should be able to put double quote marks around it.

Simple cases, such as this excecution

java CommandLineEcho"a b c"

args[0] = a b c

again behave as expected.

Unfortunately, consider somthing more realistic and complex, like this excecution:

java CommandLineEcho"cd ..\ && dir .\"

args[0] = cd ..\ && dir ."

This is unexpected: note that the text was interpreted as a single string, and the leading quote mark was dropped, which is all correct, but the final quote mark was retained, which is bizarre.

With my version of msdos, I find that I can get the correct behavior simply by dropping the final quote mark i.e. use

java CommandLineEcho"cd ..\ && dir .\

Alternatively, if I use parentheses inside the expression to group the subcommands, I can still use the logically consistent pair of quotes and everything works. For instance, excecuting

java CommandLineEcho"(cd ..\) && (dir .\)"

args[0] = (cd ..\) && (dir .\)

works.

Anybody know why msdos does the strange command line behavior that it does? Have a decent website that explains all this?

I did some web searching and could not find anything.

Typing cmd /? in my dos shell indicates that special chars can sometimes lead to funny behavior with quote marks. Unfortuately, this documentation is strictly for invocation of cmd, and not how cmd actually behaves once it is running.

Experimenting, I found that in the examples above, the offending char is actually the \ char. For instance, excecuting

java CommandLineEcho"cd .. && dir ."

args[0] = cd .. && dir .

behaves as expected.

[3454 byte] By [bbatmana] at [2007-11-26 15:38:07]
# 1
The backslash is escaping the double quote; this allows for the inclusion of a double quote in a quoted string. Try this string:"ab\"cd"It printsab"cdUse doubled backslashes to do what you want:"abcd\\"Printsabcd\
ChuckBinga at 2007-7-8 21:56:13 > top of Java-index,Desktop,Runtime Environment...
# 2

Thanks!

You're right, its that final \ escaping the final ".

Using

"cd ..\ && dir .\\"

or

"cd ..\ && dir .\ "

(note the space char between the final \ and ") are both ways that yield the desired result of

args[0] = cd ..\ && dir .\

when execute

java CommandLineEcho

bbatmana at 2007-7-8 21:56:13 > top of Java-index,Desktop,Runtime Environment...
# 3
For the record: there exist other, more capable command shells for windows too than its built-in cmd, which can handle the command-line argument excaping in a more consequent way. bash, mingw, ...
BIJ001a at 2007-7-8 21:56:13 > top of Java-index,Desktop,Runtime Environment...
# 4

>For the record: there exist other, more capable command shells for windows >too than its built-in cmd, which can handle the command-line argument >excaping in a more consequent way.

No doubt. Every sane user of dos has concluded that it may be the worst shell/os ever inflicted on humanity, and that Bill Gate$ is evil, etc etc.

But we're sometimes stuck with it.

In particular, if I want to distribute a program and claim that it will run on windows, but only if you first install cygwin or something, that can be a hurdle.Sometimes it is easier to just see if you can work with dos.

bbatmana at 2007-7-8 21:56:13 > top of Java-index,Desktop,Runtime Environment...