This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
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.
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:
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.