Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abnormal behaviour

 
Uma Kanth
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can some body expalin me why the output of the program is different
When I send the command line argument as *.Say
cmd>javac test.java
cmd>java test *

class test{
public static void main(String args[]){
for (int i = 0;i < args.length; i++) {
System.out.println(i + " " + args[i]);
}
}
}
Thanx in advance
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure I understand your question. Could you please detail a little bit more your problem. Thanks
It's possible that I move this discussion to the Java in General (Beginner) forum after answering your question since this does not really belong to the certification exam objectives.
 
Uma Kanth
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The output of the program is
The list of count and the file names in the current directory
 
Jamal Hasanov
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Uma
I think it's a bug (but couldn't find it in java Bug Database).
There're a few topics in Javarach regarfding this. One of them:-)
Regards,
Jamal Hasanov
www.j-think.com
 
Jamal Hasanov
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uma,
I found it !!!
http://www.coderanch.com/t/237887/java-programmer-SCJP/certification/as-parameter-main
This is a bug - You had to use
java test "*"
Regards,
Jamal Hasanov
www.j-think.com
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim's answer in the linked discussion kind of solves the problem.
I'm moving this to Java in General (Beginner) forum.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not a bug, it's a feature.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not even a bug with java. When you type something on the command line, the system deals with it before anything is sent to the JVM. In DOS and all flavors of Unix, the * is expanded into a list of all the files in the current directory. This is list is what the JVM then forwards on to your program as the command line arguments.
So to get a literal "*" character, you have to use the quotes as stated earlier in order to tell the system it is actually a "*" and not an operator.
HTH
Layne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic