Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2005

Tip: Looking for answers? Try searching our database.

wildcards

Thread view: 
Roedy Green - 23 Jun 2005 05:25 GMT
I just discovered something odd.  EVERY Java utility has wildcard
ability on the command line.

If you write  *.* as a parameter, you don't see "*.*" appearing in
args[0]. Instead you see a list of all the filenames and directory
names in the current directory, one per argument.

Is this new?  Who is doing this? Windows, 4NT, Java?

if you write *.* as a parameter, you will get a list of all files and
directories in the current directory beginning with a.

But if you type . you just get the .  This mechanism only seems to
kick in when you use a wildcard ? or *.

It is somewhat dangerous. *.* for a utility not prepared for it. *.*
does a directory 1-deep recursion.

Signature

Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes

Wendy Smoak - 23 Jun 2005 05:39 GMT
> If you write  *.* as a parameter, you don't see "*.*" appearing in
> args[0]. Instead you see a list of all the filenames and directory
> names in the current directory, one per argument.
>
> Is this new?  Who is doing this? Windows, 4NT, Java?

If you're doing at the command line, then I would bet the operating system
is evaluating the "*.*" before passing it as an argument to 'java'.

Signature

Wendy Smoak

Kamal Chandana - 23 Jun 2005 05:58 GMT
It's nothing to do with java. It's solely due to the command line
behavior. The console interprets the *.* as - all the files in current
directory.

So the java program receives the list of all the file names in ur
directory.
Patricia Shanahan - 23 Jun 2005 06:01 GMT
>>If you write  *.* as a parameter, you don't see "*.*" appearing in
>>args[0]. Instead you see a list of all the filenames and directory
[quoted text clipped - 4 lines]
> If you're doing at the command line, then I would bet the operating system
> is evaluating the "*.*" before passing it as an argument to 'java'.

I'm sure Roedy is aware of command line expansion. In any case, I just
did some tests, and there is something else going on. For example, using
Cygwin's bash, the line:

echo '*'

echoes an asterisk, so single quotes suppress the expansion, as they
should in bash.

On the other hand, in the same shell:

java Echo '*'

gets a directory listing, where Echo is a program that prints its
arguments. So far, I have been unable to pass an asterisk
to my program, which seems unfortunate. If I decorate it enough to
prevent expansion, the decoration gets passed through as part of the
argument. For example '"*"' is passed as "*" (three characters).

I've checked the documentation for "java", and it does not describe this
behavior.

Patricia
shakah - 23 Jun 2005 06:15 GMT
> >>If you write  *.* as a parameter, you don't see "*.*" appearing in
> >>args[0]. Instead you see a list of all the filenames and directory
[quoted text clipped - 28 lines]
>
> Patricia

For what it's worth I don't see the behavior you describe with Bash
under Linux:

jc@sarah:~/tmp$ echo $BASH_VERSION
3.00.14(1)-release

jc@sarah:~/tmp$ cat Echo.java
public class Echo {
 public static void main(String [] asArgs) {
   for(int nArg=0; nArg<asArgs.length; ++nArg) {
     System.out.println(nArg + ". '" + asArgs[nArg] + "'") ;
   }
 }
}

jc@sarah:~/tmp$ /usr/java/jdk1.5.0_01/bin/java Echo '*'
0. '*'

jc@sarah:~/tmp$ /usr/java/jdk1.5.0_01/bin/java Echo \*
0. '*'

jc@sarah:~/tmp$ /usr/java/jdk1.5.0_01/bin/java Echo "\*"
0. '\*'

jc@sarah:~/tmp$ /usr/java/jdk1.5.0_01/bin/java Echo *
0. 'abstract-test'
1. 'aide-0.10'
2. 'audacity-src-1.2.3'
3. 'axis-1_2'
etc.
Kamal Chandana - 23 Jun 2005 06:37 GMT
In widows console u can send * to the java program using "*".

public class ArgsPrinter{

    public static void main(String args[]){

        System.out.println("Number of Arguments: " + args.length);

        for(int i=0;i<args.length; i++){
            System.out.println(args[i]);
        }
    }

}

Just try to run the program as;

run ArgsPrinter "*"

result:
Number of Arguments: 1
*

- So you can input *.
Chris Uppal - 24 Jun 2005 09:58 GMT
> For what it's worth I don't see the behavior you describe with Bash
> under Linux:

That is because the java program on Unix does not do command-line expansion.
It -- correctly -- expects the invoking command processor to do whatever
expansion is required.

Under Windows (as under DOS) the architecture is different (and /very/ stupid,
IMO): individual commands are expected to do their own expansion.  That means
that the Windows version of the java program is expanding the wildcard
according to whatever rules are compiled into it, and there is nothing that the
command processor (the cygwin version of bash in Patricia's example) can do
about it.

Command-line processing under Windows is, and always has been, utterly broken.

   -- chris
SMC - 23 Jun 2005 06:16 GMT
>>>If you write  *.* as a parameter, you don't see "*.*" appearing in
>>>args[0]. Instead you see a list of all the filenames and directory
[quoted text clipped - 29 lines]
>
> Patricia

[sean@se2 tmp]$ cat e.java
public class e {

   public static void main (String args[]){
    System.out.println(args[0]);
   }
}
[sean@se2 tmp]$ java e \*
*
[sean@se2 tmp]$ java e \*.\*
*.*

Signature

Sean

One has to look out for engineers - they begin with sewing machines and
end up with the atomic bomb. --Marcel Pagnol

Harald - 23 Jun 2005 22:20 GMT
> I'm sure Roedy is aware of command line expansion. In any case, I just
> did some tests, and there is something else going on. For example, using
[quoted text clipped - 10 lines]
>
> gets a directory listing, where Echo is a program that prints its

Is the program called `java' under cygwin a wrapper script which gets
its argument passing to the real program all messed up?

 Harald.

Signature

---------------------+---------------------------------------------
Harald Kirsch (@home)|
Java Text Crunching: http://www.ebi.ac.uk/Rebholz-srv/whatizit/software

Patricia Shanahan - 23 Jun 2005 22:35 GMT
>>I'm sure Roedy is aware of command line expansion. In any case, I just
>>did some tests, and there is something else going on. For example, using
[quoted text clipped - 13 lines]
> Is the program called `java' under cygwin a wrapper script which gets
> its argument passing to the real program all messed up?

Good question, but it doesn't seem to be:

[510] which java
/cygdrive/c/Program Files/Java/jdk1.5.0/bin/java

/cygdrive/c is the unix-like path Cygwin gives to C:, so that is
C:/Program Files/Java/jdk1.5.0/bin/java, my JDK1.5 java.exe.

I also tested, before posting, with 1.4 by forcing the java.exe file
name, and it behaved the same way.

Patricia
Hemal  Pandya - 24 Jun 2005 09:20 GMT
It seems to have something to do with bash, but I wonder what. The
results are different on Windows command interpreter and on cygwin
bash:

C:\java>java Echo *
0. 'ant'
1. 'Echo.class'
2. 'Echo.java'
3. 'Echo.java~'
4. 'game'
5. 'test'

C:\java>java Echo '*'
0. ''*''

C:\java>java Echo "*"
0. '*'
Roedy Green - 24 Jun 2005 01:00 GMT
>I'm sure Roedy is aware of command line expansion.

I wasn't until I did some experiments.  I wrote a little essay, as
usual on what I found out about command line expansion.

see http://mindprod.com/jgloss/wildcard.html

Signature

Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes

Raymond DeCampo - 24 Jun 2005 03:28 GMT
>>I'm sure Roedy is aware of command line expansion.
>
> I wasn't until I did some experiments.  I wrote a little essay, as
> usual on what I found out about command line expansion.
>
> see http://mindprod.com/jgloss/wildcard.html

Roedy,

Portions of the essay are somewhat misleading.  In Windows, it is not
the command interpreter or BAT language that expands the wildcards.  It
is the application that is invoked that does the expanding.  This
explains why wildcard expansion in Windows can be inconsistent from
program to program.

I am confused by one part of your essay:

- Oddly if you pass a wild card to any Java utility, the command
- processor expands the wildcards so that you program for *.* will see
- list of all the directories and all the files, one per arg[i] slot.

Unless the directories have the a period character in the name, they
should not match the *.* wildcard expression.  If this does happen
perhaps you have found a bug in the JVM.

HTH,
Ray

Signature

XML is the programmer's duct tape.

Chris Head - 24 Jun 2005 18:39 GMT
>>> I'm sure Roedy is aware of command line expansion.
>>
[quoted text clipped - 23 lines]
> HTH,
> Ray

Hi,
At least in old DOS, *.* actually meant every file, whether or not it
contained a dot. This would be because every file "implicitly" had a dot
in it. Meanwhile, just asking for * I think didn't match anything.

Chris
Roedy Green - 26 Jun 2005 02:24 GMT
>Portions of the essay are somewhat misleading.  In Windows, it is not
>the command interpreter or BAT language that expands the wildcards.  It
[quoted text clipped - 11 lines]
>should not match the *.* wildcard expression.  If this does happen
>perhaps you have found a bug in the JVM.

try this out. I think you will be surprised.

/**
* Tests what sort of wildcard expansion happens automatically.
* It is very simple. Just look at the main method code to understand
* what it does and what you could use it for.
*
* try with:
* java WildcardExpansion *.*
* java WildcardExpansion "*.*"
* java WildcardExpansion s*.*
* java WildcardExpansion .
* java WildcardExpansion *.html
* java WildcardExpansion afile.txt anotherfile.txt
* java WildcardExpansion d?mmy.* (where you have dummy.* files and a
dummy dir)
* java WildcardExpansion t?mmy.* (where you don't have tummy.* files
or a tummy dir)
*
* My discoveries: For Win2K:
* Wildcards are automatically expanded before your program sees them.
* Who does it? Presumably the command processor or a Sun class.
* You get a list of matching files INCLUDING matching directory
names,
* but not the files in those subdirectories. Since directories
normally
* don't have extensions, you won't see the problem with *.html but
you will
* with s*.* or *.*.
* If the wildcard does not match anything, you get the raw wildcard.
* The wildcard in quotes gives you the raw wildcard.
* Author Roedy Green
* Version 1.0
*/

public class WildcardExpansion
  {
  /**
   * Echo command line arguments, along with any automatic expansion
   * Can be used to test wildcard expansion, quoting, or any other
basic features
   * features of command line parsing.
   *
   * @param args whatever you want to test.
   */
  public static void main ( String[] args )
     {
     System.out.println ( args.length + " arguments" );

     for ( int i=0; i<args.length; i++ )
        {
        // [ ] help prove no lead/trail blanks tabs etc.
        System.out.println( "[" + args[i] + "]" );
        }
     }
  }

Signature

Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes

Roland - 26 Jun 2005 11:27 GMT
>>Portions of the essay are somewhat misleading.  In Windows, it is not
>>the command interpreter or BAT language that expands the wildcards.  It
[quoted text clipped - 68 lines]
>       }
>    }

Yep, Win2000's cmd.exe seems to expand wildcard commandline arguments,
not the JVM or the java.exe launcher. The expansion of 'd?mmy.*' to
include a directory without a dot in its name seems to be consistent
with the result of the command
    dir d?mmy.*

The src.zip file in the JDK directory contains the C source of the Java
launcher (java.exe / javaw.exe). As far as I could see, the launcher
does not do any wildcard expansion. It does take care of handling the
launcher specific argument (like -classpath, -Xmx128m,  -showversion,
etc.) and strips them from the arguments that eventually get passed to
the Java main method.

Looking at the C source also revealed that you can set the environment
variable _JAVA_LAUNCHER_DEBUG to enable logging of the launcher. Here's
an example of my trace:

C:\temp>dir d?mmy.*
 Volume in drive C is NTFS
 Volume Serial Number is E8DA-EAA9

 Directory of C:\temp

26-06-2005  10:01                   49 dammy.txt
26-06-2005  11:59       <DIR>          dummy
31-12-2004  14:49                  957 Dummy.java
26-06-2005  10:00                   52 dummy.txt
               3 File(s)          1.058 bytes
               1 Dir(s)   1.285.324.800 bytes free

C:\temp>echo %CLASSPATH%
.;.;.;c:\PROGRA~1\Java\JMF21~1.1\lib\sound.jar;c:\PROGRA~1\Java\JMF21~1.1\lib\jmf.jar;c:\PROGRA~1\Java\JMF21~1.1\lib;%systemroot%\java\classes;.

C:\temp>set _JAVA_LAUNCHER_DEBUG=1

C:\temp>java -showversion -cp . WildcardExpansion d?mmy.*
----_JAVA_LAUNCHER_DEBUG----
Version major.minor.micro = 1.5.0
JRE path is C:\Program Files\Java\jre1.5.0
jvm.cfg[0] = ->-client<-
jvm.cfg[1] = ->-server<-
jvm.cfg[2] = ->-hotspot<-
    name: -hotspot  vmType: VM_ALIASED_TO  alias: -client
jvm.cfg[3] = ->-classic<-
jvm.cfg[4] = ->-native<-
jvm.cfg[5] = ->-green<-
590 micro seconds to parse jvm.cfg
Default VM: client
JVM path is C:\Program Files\Java\jre1.5.0\bin\client\jvm.dll
4020 micro seconds to LoadJavaVM
JavaVM args:
    version 0x00010002, ignoreUnrecognized is JNI_FALSE, nOptions is 3
    option[ 0] =
'-Djava.class.path=.;.;.;c:\PROGRA~1\Java\JMF21~1.1\lib\sound.jar;c:\PROGRA~1\Java\JMF21~1.1\lib\jmf.jar;c:\PROGRA~1\Java\JMF21~1.1\lib;%systemroot%\java\classes;.'
    option[ 1] = '-Djava.class.path=.'
    option[ 2] = '-Dsun.java.command=WildcardExpansion dammy.txt dummy
dummy.java dummy.txt'
java version "1.5.0_04"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_04-b05)
Java HotSpot(TM) Client VM (build 1.5.0_04-b05, mixed mode)

88566 micro seconds to InitializeJVM
Main-Class is 'WildcardExpansion'
Apps' argc is 4
    argv[ 0] = 'dammy.txt'
    argv[ 1] = 'dummy'
    argv[ 2] = 'Dummy.java'
    argv[ 3] = 'dummy.txt'
34159 micro seconds to load main class
----_JAVA_LAUNCHER_DEBUG----
4 arguments
[dammy.txt]
[dummy]
[Dummy.java]
[dummy.txt]

C:\temp>
Signature

Regards,

Roland de Ruiter
` ___      ___
`/__/ w_/ /__/
/  \ /_/ /  \

Raymond DeCampo - 26 Jun 2005 16:10 GMT
>>> Portions of the essay are somewhat misleading.  In Windows, it is not
>>> the command interpreter or BAT language that expands the wildcards.  
[quoted text clipped - 147 lines]
>
> C:\temp>

This analysis proves nothing.  If you had bothered to look at the link
for MSDN on command line expansion you would have learned that MS
provides a library that does the command line wildcard expansion and
that all you have to do is link to it for the wildcards to be expanded
_before_ they are passed to your program.  So, you cannot tell if
wildcards are expanded just by looking at the source code and you cannot
 assume that the command processor is expanding wildcards by looking at
what is passed to main().

Ray

Signature

XML is the programmer's duct tape.

Raymond DeCampo - 26 Jun 2005 16:05 GMT
>>Portions of the essay are somewhat misleading.  In Windows, it is not
>>the command interpreter or BAT language that expands the wildcards.  It
[quoted text clipped - 13 lines]
>
> try this out. I think you will be surprised.

As I said before, if directories without '.' in the name are matched by
*.*, that is a bug in the JVM.

BTW, if you would like to prove to yourself that the expansion is done
by the JVM and not the command processor, do not execute your programs
from the command processor.  Use Runtime.exec() to invoke the java.exe
directly.

Ray

Signature

XML is the programmer's duct tape.

Raymond DeCampo - 24 Jun 2005 02:11 GMT
>>> If you write  *.* as a parameter, you don't see "*.*" appearing in
>>> args[0]. Instead you see a list of all the filenames and directory
[quoted text clipped - 27 lines]
> I've checked the documentation for "java", and it does not describe this
> behavior.

This has to due with the way wildcard expansion is handled in Windows.
Wildcard expansion is not handled by the command interpreter, as in Unix
shells.  Wildcard expansion is built into the console application (those
of you familiar with Visual C++ will know t=what the settings are to do
this).

<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/bas
ic_22.asp
>

HTH,
Ray

Signature

XML is the programmer's duct tape.

Roedy Green - 26 Jun 2005 02:30 GMT
><http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/bas
ic_22.asp
>

So this implies the JRE is calling setargv or equivalent for you. In
other words, the screw up is Sun's fault, not Microsofts that you
can't tell a wildcard expansion asking for ONLY *.* files, from a list
of directories and files to process keyed out longhand.

Signature

Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes

Raymond DeCampo - 26 Jun 2005 17:29 GMT
>><http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/bas
ic_22.asp
>
>
> So this implies the JRE is calling setargv or equivalent for you. In
> other words, the screw up is Sun's fault, not Microsofts that you
> can't tell a wildcard expansion asking for ONLY *.* files, from a list
> of directories and files to process keyed out longhand.

Huh?  I expected you to be saying the opposite.

It is possible that to override setargv within your program by defining
it yourself.  However, from the source code that Sun provides, it
appears they are not doing that.  So the fault would lie in the default
implementation of setargv and my claim that it is a JVM bug was premature.

Ray

Signature

XML is the programmer's duct tape.

shakah - 23 Jun 2005 06:05 GMT
> I just discovered something odd.  EVERY Java utility has wildcard
> ability on the command line.
[quoted text clipped - 18 lines]
> Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
> http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

> Canadian Mind Products, Roedy Green.
> See http://mindprod.com/iraq.html photos of Bush's war crimes

I'd think it is your command shell doing filename expansion, analogous
to Bash's globbing:
 http://www.tldp.org/LDP/abs/html/globbingref.html
"." - 23 Jun 2005 21:51 GMT
> I just discovered something odd.  EVERY Java utility has wildcard
> ability on the command line.
[quoted text clipped - 13 lines]
> It is somewhat dangerous. *.* for a utility not prepared for it. *.*
> does a directory 1-deep recursion.

It is Windows that is doing it. I believe since 5.0 (Windows 2000) the
command line will expand the wildcards * and ? before passing to a
program.

> --
> Bush crime family lost/embezzled $3 trillion from Pentagon.
[quoted text clipped - 3 lines]
> Canadian Mind Products, Roedy Green.
> See http://mindprod.com/iraq.html photos of Bush's war crimes

Signature

Send e-mail to: darrell dot grainger at utoronto dot ca

Raymond DeCampo - 24 Jun 2005 03:34 GMT
. wrote:

>>I just discovered something odd.  EVERY Java utility has wildcard
>>ability on the command line.
[quoted text clipped - 17 lines]
> command line will expand the wildcards * and ? before passing to a
> program.

I posted the following link as evidence that it is not the command
interpreter but the application that expands wildcards:

<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/bas
ic_22.asp
>

If this did indeed change in Windows 2000, I would like to read some
documentation on that.  Can you provide a link?

(While it is true that I do not think this is the case, my request is
genuine.)

Thanks,
Ray

Signature

XML is the programmer's duct tape.

Roedy Green - 26 Jun 2005 02:31 GMT
>I posted the following link as evidence that it is not the command
>interpreter but the application that expands wildcards:
>
><http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/bas
ic_22.asp
>

It depends how you define application. It is definitely no the java
app you write that does it.  The argument are expanded before they hit
MAIN. It seems likely the JRE is doing it.

Signature

Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes

Raymond DeCampo - 26 Jun 2005 17:32 GMT
>>I posted the following link as evidence that it is not the command
>>interpreter but the application that expands wildcards:
[quoted text clipped - 4 lines]
> app you write that does it.  The argument are expanded before they hit
> MAIN. It seems likely the JRE is doing it.

Well, application here means from the perspective of the command line
interpreter, which is the JRE.  Yes, the arguments are expanded before
they hit main(), either the main() in Java or the C main() function for
the JRE.  The wildcard expansion is done by setargv before the C main()
function is invoked.  You control whether you want this for your (C/C++)
application by linking with a specific library that contains setargv.

Ray

Signature

XML is the programmer's duct tape.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.