Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Asterisk as a command-line argument

 
Michael Matola
whippersnapper
Ranch Hand
Posts: 1817
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most shells have some notion of wild-card matching. This occurs under Unix as well. It's part of the command shell interpreter.
 
Michael Matola
whippersnapper
Ranch Hand
Posts: 1817
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope
The arguments belong to the command interpreter. It gets first dibs, and decides what information it feels like passing along to whatever program
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Unix we use "hard" quotes (single quote marks) to escape shell interpretation. Maybe try that on Windoze?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic