aspose file tools*
The moose likes Beginning Java and the fly likes Asterisk as a command-line argument Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Asterisk as a command-line argument" Watch "Asterisk as a command-line argument" New topic
Author

Asterisk as a command-line argument

Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1740
    
    2
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

Joined: Jan 07, 2002
Posts: 2205
Most shells have some notion of wild-card matching. This occurs under Unix as well. It's part of the command shell interpreter.


Rob
SCJP 1.4
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1740
    
    2
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

Joined: Jan 07, 2002
Posts: 2205
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

Joined: Dec 10, 2001
Posts: 7023
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?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

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
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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.
 
Consider Paul's rocket mass heater.
 
subject: Asterisk as a command-line argument
 
Similar Threads
OO Calculator -- a Tutorial
Someone can explain this? Why Output is so strange?
Command Line
Windows cmd line args with wildcards
Counting uppercase? Part 2!