my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Command line argument Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Command line argument" Watch "Command line argument" New topic
Author

Command line argument

mert �zkaya
Ranch Hand

Joined: Jan 26, 2006
Posts: 33
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

Joined: Oct 29, 2001
Posts: 8843
    
    7

Garrett gave a great answer to your previous question about command line arguments here. What don't you understand about his answer?


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
mert �zkaya
Ranch Hand

Joined: Jan 26, 2006
Posts: 33
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

Joined: Jan 26, 2006
Posts: 33
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

Joined: Sep 16, 2005
Posts: 1780
Here is the discussion of command line arguments from Sun's tutorial: http://java.sun.com/docs/books/tutorial/essential/attributes/cmdLineArgs.html


There is no emoticon for what I am feeling!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11170
    
  16

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?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Command line argument
 
Similar Threads
Cattle Drive- Assignment 4a
Creating a folder in C drive root
head first ejb pg:57 client problem!!!please help urgent
Command LIne Arguments
Please help