• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Behaviour of main() method

 
Ranch Hand
Posts: 75
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 75
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 75
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Lasagna is spaghetti flvored cake. Just like this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic