22

I have a class like -

public class Test{
 public static void main(String[] args){
 for(String s: args){
 System.out.println(s);
 }
 }
}

When I ran this program like> java Test * it print the names of all files in that folder.
I don't know whether JVM or windows passes this array instead of ' * ', I would like to know whether this program behaves same on the other platforms(other than windows) and why it behaves in this way? is there any purpose or reason for this?

NOTE:
It doesn't break any of my code but I'm just curious.

asked Mar 20, 2011 at 13:55
0

3 Answers 3

14

I originally thought that this was just the command line shell performing filename expansion. That's what I'd expect under a Unix-like system, but not Windows... but the same occurs for me under Windows, too.

It doesn't occur when I write the equivalent C# code... so I suspect it's something in the Java executable launcher doing it. (Note that javaw.exe behaves the same way as java.exe.)

It certainly came as a surprise to me, and I didn't think older versions of Java did this on Windows, although this mailing list post from December 2000 suggests I'm wrong. (I'm using an OpenJDK 1.7 JRE.)

I can't find any description of this general case in the Java Windows documentation - it mentions expansion of classpath entries, but not general arguments.

answered Mar 20, 2011 at 13:57
Sign up to request clarification or add additional context in comments.

5 Comments

yes.. I tried with java 5 and java 6 and it behaves exactly same as I mentioned.
I think they are emulating the * expansion in the java loader on systems like Windows where the usual shell doesn't do it.
I just searched quite a bit in the source, and didn't find anything relevant here :-(
In UNIX it's the job of the shell (as in /bin/bash or whatever you are using) to do this sort of thing. Windows passes everything after the executable name to the executable. There is a method call (GetSomethingSomething) which interprets the command line in a standard way. However, that call my be hidden by library magic, with argv and argc appearing with underscores as _argv and _argc. Not a fan of Windows.
(Remember Windows calls WinMain, not main. On UNIX, you can call a binary directly, and it won't have the arguments mangled. / Doing a search actually finds GetCommandLine msdn.microsoft.com/en-us/library/ms683156(v=vs.85).aspx to get the string, and CommandLineToArgW msdn.microsoft.com/en-us/library/bb776391(v=vs.85).aspx to parse.
1

Not until recently (i guess in Java 6) a new feature was added to Java, where '*' is translated to all files in the current directory. This was mainly to avoid need for passing long class paths and giving a platform independent way to pass all jar files in a folder to a classpath.

java -cp "lib/*" <mainClass>

lib/* would be tranlated to list of file separated by the classpath separator for that platform (i.e. : for unix like and ; for windows) passed to -cp property.

I think this might be the reason for the behavior that you see. I assumed that it was only for specifying classpaths but now I think I was wrong then.

answered Mar 21, 2011 at 7:50

Comments

0

I think command line, first interpret the special operators before passing to java file.

e.g.

java Test * > test.txt
java Test Test.????
java Test ^

I think its not specific to java.

answered Mar 21, 2011 at 5:58

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.