Java command line arguments. Using * as an argumen

2019-01-15 15:45发布

This question already has an answer here:

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

2条回答
走好不送
2楼-- · 2019-01-15 16:09

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.

查看更多
可以哭但决不认输i
3楼-- · 2019-01-15 16:13

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.

查看更多
登录 后发表回答