While goofing around writing a Java program that happens to sometimes take an asterisk (to indicate multiplication) as a command-line argument, I noticed some flaky behavior: I could only get the program to accept an asterisk if I put it in quotes. Turns out, the command shell in Windows 2000 is expanding the * in the argument list to a list of files in the working directory before invoking java.exe. Run this short program with the following arguments to see the effect: java ArgPrinter someArg * anotherArg java ArgPrinter someArg "*" anotherArg
Crazy, no? I'm curious if folks on operating systems other than Windows run into this same sort of effect. [ March 09, 2002: Message edited by: Michael Matola ]
Most shells have some notion of wild-card matching. This occurs under Unix as well. It's part of the command shell interpreter.
posted 17 years ago
I guess what was tripping me up was the idea that the expansion of the wildcard must be happening before the interpreter even knows what program/command it's invoking. These are my program's arguments after all, not something the operating system should care about!
posted 17 years ago
Nope The arguments belong to the command interpreter. It gets first dibs, and decides what information it feels like passing along to whatever program
This problem has reared its ugly head for me. I'm running Windows XP, and whether I use the asterisk in quotes or as just a plain old asterisk for my argument, it behaves as Michael experienced on Windows 2000 while not in quotes. Anybody know of some escape character sequence to be able to pass the asterisk as an argument to a Java program?
In Unix we use "hard" quotes (single quote marks) to escape shell interpretation. Maybe try that on Windoze?
Make visible what, without you, might perhaps never have been seen. - Robert Bresson
posted 16 years ago
Well, leave it to Windows to provide an inconsistent and seemingly unexplainable experience. Since my original report, I've reinstalled Windows XP (from the same installation CD) and I now experience different results. Enclosing the asterisk in double quotes successfully passes only the asterisk character as a String parameter. Enclosing the asterisk in either of the single quotes - ' or ` - passes a String argument composed of the asterisk surrounded by the single quote characters. Go figure.