• Post Reply Bookmark Topic Watch Topic
  • New Topic

Behaviour of main() method  RSS feed

 
Ranch Hand
Posts: 61
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Folks,

I just need explanation of this below simple code.



This code compiles fine but when I run it it throws following error

D:\Java\test>java MainTest
Exception in thread "main" java.lang.NoSuchMethodError: main


It says that JVM is not able to recognize this main method which is quite obvious because internally the line


is converted into



Hence my question is why compiler didn't recognize it ?

Help in this matter is highly appreciated.

Thanks...





 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Khuzema Dharwala wrote:Hence my question is why compiler didn't recognize it ?

Your compiler recognised it just fine, which is why you didn't get any errors.

Question: What is the signature of the main() method? If need be, go back to your book, or the tutorials, and check.

Winston
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler didn't complain because that's an entirely valid method signature. You're allowed to have an argument which takes an array of arrays. But that doesn't mean it's a main method that will be recognised by the JVM as a valid start point.

You can use main(String[] args), or you can use main(String... args) (which compiles to the same thing). But main(String[]... args) is something different, as you've seen.
 
Khuzema Dharwala
Ranch Hand
Posts: 61
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks Winston for your reply what I actually mean was why compiler didn't recognized that it is not a valid main method that JVM will not accept.

Thanks Matthew for your explanation I wasn't aware of the fact that main method is allowed to take array of arrays.

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Khuzema Dharwala wrote:Thanks Matthew for your explanation I wasn't aware of the fact that main method is allowed to take array of arrays.

Erm...what he actually said was that the main method is NOT allowed to take an array of arrays - at least, not if you want it to be recognised by the JVM. You can overload any method; but only one signature of main() is recognised by the java command.

Winston
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Khuzema Dharwala wrote:
Thanks Winston for your reply what I actually mean was why compiler didn't recognized that it is not a valid main method that JVM will not accept.


How would the compiler know that you don't have another method that calls this method? Are you saying that because the method is call main(), the compiler should assume that you will actually use it as a JVM main method? It is perfectly valid to have classes without main methods, which is what you have here.

Henry
 
Khuzema Dharwala
Ranch Hand
Posts: 61
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Valid point Henry I actually thought that "because the method is call main(), the compiler should assume that you will actually use it as a JVM main method?" but my doubt is clear now thanks much appreciated.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!