The Artima Developer Community
Sponsored Link

Java Answers Forum
Duped

8 replies on 1 page. Most recent reply: Apr 14, 2003 3:24 AM by Rahul

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 8 replies on 1 page
Rahul

Posts: 52
Nickname: wildhorse
Registered: Oct, 2002

Duped Posted: Apr 11, 2003 3:55 AM
Reply to this message Reply
Advertisement
I can't figure out why this program lists all the file names in the directory where it resides if I pass '*' as an argument in the command line.

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

Can anybody explain ?


Alex S

Posts: 27
Nickname: andu
Registered: Mar, 2003

Re: Duped Posted: Apr 11, 2003 5:24 AM
Reply to this message Reply
The operating sistem will expand '*' (regular expression) in a list of all files in the current directoriy. Therefore, when your java program will start, it will have a command line formed by a list of files. So, it is not java but operating sistem.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Duped Posted: Apr 11, 2003 8:40 AM
Reply to this message Reply
Actually, I think it is Java doing it to some extent. Some operating systems do that for you and some don't. For instance, Windows doesn't, but when you run a Java program with wildcards, it gets "globbed" and shows all the individual filenames that match, whereas when you run a similar C program, it doesn't.

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: Duped Posted: Apr 11, 2003 9:07 AM
Reply to this message Reply
Matt Gerrans,

When you say windows doesn't which windows you are reffereing to? Becuase I tried it with Windows 2000 and it does expand the * to the file names in the directory.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Duped Posted: Apr 11, 2003 1:15 PM
Reply to this message Reply
In a Java app or a C/C++ app?

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: Duped Posted: Apr 12, 2003 9:20 AM
Reply to this message Reply
I did not try it with any specific application. I just tried in the command prompt with a DOS command.

Ex : 1) goto a directory
2) copy * d:\
3) this statement copies all the files from the current directory to d:\

for me that implies that * is being expanded by the Windows OS to all the files in the directory...

Isn't it?

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Duped Posted: Apr 12, 2003 3:53 PM
Reply to this message Reply
Nope. The copy command is taking care of that itself. Try compiling this C++ program:
#include <iostream.h>
void main( int argc, char *argv[] )
{
   cout << "You supplied " << (argc-1) << 
           " arguments (argc is " << argc << "):" << endl;
   for( int i = 0; i < argc; i++ )
      cout << "argv[" << i << "] => " << argv[i] << endl;
}

and this Java program:
public class CmdLine
{
   public static void main( String args[] )
   {
      System.out.println( "You supplied " + args.length +
                          " arguments (args.Length is " +
                          args.length + "):" );
      for( int i = 0; i < args.length; i++ )
         System.out.println( "args[" + i + "] => " + args[i] );
   }
}

Now try running the C++ program with the command line *.* and the Java version with the same command line. The C++ program will have this output:
You supplied 1 arguments (argc is 2):
argv[0] => D:\code\cmdline.exe
argv[1] => *.*

but the Java program will automatically expand out the *.* to match all the files in the current directory, like so:
You supplied 196 arguments (args.Length is 196):
args[0] => AddressInfo.java
args[1] => AddressInfo2.java
args[2] => AddressInfo3.java
. . .

Adam Duffy

Posts: 168
Nickname: adamduffy
Registered: Feb, 2003

Re: Duped Posted: Apr 14, 2003 2:55 AM
Reply to this message Reply
Moral of the story?

Use Properties files or have your command line arguments within quotes.

Adam

Rahul

Posts: 52
Nickname: wildhorse
Registered: Oct, 2002

Re: Duped Posted: Apr 14, 2003 3:24 AM
Reply to this message Reply
Thank you guys.

Flat View: This topic has 8 replies on 1 page
Topic: Java application in Maximized state Previous Topic   Next Topic Topic: Urgetnt Problem on GUI

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use