Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange Behavior of Java Argument *

Tags:

java

I wrote this class :

public class ListArg {
    public static void main(String args[])
    {
        for(int i=0;i<args.length;i++)
        {
            System.out.println(args[i]);
        }
    }
}

javac ListArg.java // compiled class

I compiled above class and run like : java ListArg *

But ListArg is displaying current directory contents on console and not "*".

like image 736
Nandkumar Tekale Avatar asked Jan 11 '12 11:01

Nandkumar Tekale


1 Answers

EDIT: Looks like I was wrong and this might be Java doing it after all. If you're using a Unix shell, this is probably just the shell performing "globbing". However, it appears to do the same thing on Windows, which surprises me (as the Windows command line doesn't perform globbing by default).

Unfortunately, on Windows the normal quoting appears to give you a quoted argument, i.e. if you print args[0] having run

java ListArg '*'

it will include the single quote. I'm investigating whether there's a way of disabling this... although that would work on a Unix shell.

EDIT: Hmm... no luck so far in managing to have a string of just a star in Windows :(

like image 83
Jon Skeet Avatar answered Oct 27 '22 16:10

Jon Skeet