• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Command line argument

 
mert �zkaya
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi veeryone,
I made deep researches about Command line argument but I still could not understand itsfunctions on main method. So if anyone know , please inform me.

Thanks,

Mert
 
Joe Ess
Bartender
Pie
Posts: 9266
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Garrett gave a great answer to your previous question about command line arguments here. What don't you understand about his answer?
 
mert �zkaya
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Garrett's code is a little bit complicated for me. Can anyone explain command line argument by giving more simple examples Garrett ??

Thanks,

Mert
 
mert �zkaya
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Garrett's code is a little bit complicated for me. Can anyone explain command line argument by giving more simple examples Garrett ??

Thanks,

Mert
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the discussion of command line arguments from Sun's tutorial: http://java.sun.com/docs/books/tutorial/essential/attributes/cmdLineArgs.html
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12102
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want to know why we write (String args[]) in every main method?

most programs you don't just tell to "start". you tell them to start, and give them additional parameters on HOW to start. think about things like copy or cp you've probably used. you've probably done this:

copy file.a file.b

"copy" is the program, "file.a" is a parameter, and so is "file.b"

sure, the program could have been written so that you just say "copy". then it prompts you for "source file??: " and waits for you to type something in. then it says "destination??: " and waits for you to type something in...

but everybody knows you need both these arguments. so, a way was written to parse the whole line you type in, and figure the parts out. the first "word" is the program to run. the rest of the words are arguments to that program.

Java does this by taking everything AFTER the name of the program, and puts them into a string array. if i type

java myCopyProgram file.a file.b

the strings "file.a" and "file.b" are put into the array you declare in your main method. this is USUALLY named args, but you could have

public static void main (String inputs[])

or really any name for it you want.

if your program doesn't need any arguments, you'll never use it. Even if you don't use it, if the user types in some stuff, it'll go in that array. HOW you use them is up to you.

does that help?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic