Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does * mean in java's main args list?

Tags:

java

I wrote a class like this and named it Solution.java.

public class Solution {
    public static void main(String[] args) {
        System.out.println(args.length);
    }
}

BUt when I run it in Terminal, I got a result like this:

>  /Users/WangWei  java Solution *
18
>  /Users/WangWei

Why 18?

like image 249
David Avatar asked Dec 20 '13 12:12

David


Video Answer


1 Answers

That's probably the number of files in your working directory.

The result of * is not specific to Java. It is specific to the environment you are working in, i.e. the working directory and the kind of shell (Windows command prompt, bash, ...) you are using to run the java command. This is because the shell processes and evaluates the command line before starting the process. It replaces the *.

To preserve a * as a command line argument, you need to quote it:

java Solution '*'
like image 181
Daniel S. Avatar answered Oct 05 '22 02:10

Daniel S.