wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Passing String array as argument to main() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Passing String array as argument to main() method" Watch "Passing String array as argument to main() method" New topic
Author

Passing String array as argument to main() method

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Hi all
Please explain why do we have to pass a String array as an argument to the main method.

Regards
Mansukhdeep


~ Mansukh
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Read about Command-Line Arguments.


[My Blog]
All roads lead to JavaRanch
Naresh Shanmugam
Ranch Hand

Joined: Jul 16, 2010
Posts: 84
Only with that you can pass arguments to the main program. If it would have been "String" you can just pass one argument, since it is a string array you can pass "n" number of arguments as strings to your program.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

A single String would be possible as well; Visual Basic does it that way. However, that means that you have to do the parsing yourself. Trust me, it's not something you want to do. Splitting on spaces will not work because quoting (with ") allows spaces inside arguments. And by escaping it with \ you can even put " inside arguments, including arguments already surrounded by quotes. For example, "arg with \" and spaces" is one single argument.

Fortunately, by using a String[], you don't need to worry about it. The shell (command prompt) does it all for you, passes that to the JVM, which in turn passes it to your application.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Naresh Shanmugam
Ranch Hand

Joined: Jul 16, 2010
Posts: 84
That is a valuable point Rob..
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Christophe Verré wrote:Read about Command-Line Arguments.


That is fine Chris. I went through the page. My question is that why is it that we cannot do without passing a String[] array to the main thread? Why is it that the JVM fails to recognize the main() method & throws an exception saying:

java.lang.NoSuchMethodError: main
Exception in thread "main"

if I try to run a Java Application witout passing that String [] args? Please elaborate.

Regards
Mansukhdeep
Naresh Shanmugam
Ranch Hand

Joined: Jul 16, 2010
Posts: 84
Consider you have a method some thing like this

myMethod(int){ }

You can invoke this method only by passing an int like

myMethod(5) (or)
myMethod(a) // "a" holding some int value

You can never invoke this method like this

myMethod("Hello");

Similary JVM always invokes the main method which takes String array as an argument. If it cant find one then a exception would be thrown.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

That's the way Java is designed. It could also have used a no-argument main method if it cannot find the one we all know and love, but that would require an extra check. In pseudo code:
In practice it will be a bit harder. The looking up of the method already throws the error. So the actual pseudo code would be more like this:
That said, this try-catch is Java code. The JVM doesn't use Java to call the main method, it uses JNI. The code in the try-block is pretty straightforward in JNI, it's the catching that is the problem.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38016
    
  22
You can even insert new lines
"A single argument
with
escaped
\" and spaces
and new lines

"
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

If the console supports it.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38016
    
  22
I usually use Linux, where the console supports everything. You think it might not work on Windows?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

You can't type in those enters in the console. After all, enters finish the command. It may work through scripts, haven't tested that.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38016
    
  22
When I tried it, I got a > prompt after the new lines, until I closed the String with a quote ".
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

On Windows XP it will immediately execute once you press enter, even if you have only an opening quote (which does get chopped off). Maybe Windows Vista / 7 fixed this.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Passing String array as argument to main() method
 
Similar Threads
MAIN method
why (String args[]) ??
arg type for main method
main method signature..
Problem in understanding how JVM triggers Main method