• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing String array as argument to main() method

 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
Please explain why do we have to pass a String array as an argument to the main method.

Regards
Mansukhdeep
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read about Command-Line Arguments.
 
Naresh Shanmugam
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Naresh Shanmugam
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is a valuable point Rob..
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can even insert new lines
"A single argument
with
escaped
\" and spaces
and new lines

"
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the console supports it.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I usually use Linux, where the console supports everything. You think it might not work on Windows?
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I tried it, I got a > prompt after the new lines, until I closed the String with a quote ".
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic