This question already has an answer here:
- The issue of * in Command line argument 5 answers
I am trying to run a java calculator application from command line. the parameters are as follows : operator operand1 operand2. I can successfully run the java program for + and - .
e.g.
java calc + 2 4
java calc - 10 4
But when I try to run
java * 2 5
System.out.println(args[0]);
System.out.println(args[1]);
System.out.println(args[2]);
gives output:
.classpath
.project
.settings
I found out by trial and error that using single quotes( '*' ) solved my problem.
SO i have two questions now.
1. Is using single quotes the right way to do it? (java calc '*' 2 5 )
2. What is the meaning of * in the java command line? (I've tried to find this on internet but didn't find much help)
Thanks, Punit
If you quote your command arguments, the shell will not expand them to filenames. '*' has no special meaning to java it is the shell that processes this input.
It's not Java, it's the shell (
cmd
if you're on Windows) you are using that interprets*
as "all files and folders in the current directory".So when your write:
You will actually give your program the following arguments:
Where
file_1 ... file_n
are all files (and folders) in the current directory).If you do not want your shell to interpret
*
as all files you need (as you have noticed) to quote that argument.