Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java command line arguments. Using * as an argument for multiplication [duplicate]

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

like image 269
Gadenkan Avatar asked Sep 16 '11 06:09

Gadenkan


2 Answers

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:

java calc * 2 5

You will actually give your program the following arguments:

java calc file_1 file_2 ... file_n 2 5

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.

like image 101
dacwe Avatar answered Oct 11 '22 19:10

dacwe


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.

like image 23
stacker Avatar answered Oct 11 '22 18:10

stacker