• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

main(String args[])?

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Am new to java ranch and scjp.
Help me to understand this.

public static void main(void){}

when we use this why does it through runtime exception.
java.lang.NoSuchMethodError: main

Thanks
Praveen SP
 
Ranch Hand
Posts: 99
Mac Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java main method has a must follow signature, which is


Anything except that doesn't considered a main method. In your code main() method has a different signature but a valid method signature, so no compile time error. But when Java tuntime checks for the main method it couldn't find the method and given the runtime exception.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

public static void main(String[] args) is the syntax(i.e., in Java the main method will take an argument of array of String Objects) if you write
public static void main(void) the compiler will check the syntax.. that is why your getting that.
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more thing (you might be knowing this already) we can give any argument name instead of "args"

So following are valid main method signatures which java will call on executing a program:



Murali...
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
every language has its own syntax that is not syntax of main method
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can also use "final" with main method !!

public final static void main(String[] args) {}
[ July 20, 2007: Message edited by: Priyam Srivastava ]
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Having a method named main() does not really save the purpose. If at all you have a method named main() whose signature exactly matches to that of JVM's expectation, you will be through.

While compiling,



All of them are legal as such they are compliant to the JLS.

But when you think of running the program by invoking the main() method which the JVM expects, your method should exactly match



If the JVM is unable to find such a method in the class which is invoked along with the "java" executable as



It throws a "NoSuchMethodError" because it does not find one.

Note: The order of the keywords "public" "static" may vary. The name of the String array argument "args" also can be of any legal identifier name.

HtH.
 
Praveen Seluka
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all

I am new to java ranch and I am very happy seeing reply in few hours.
Thanks for clearing the doubt.


Praveen SP
 
PI day is 3.14 (march 14th) and is also einstein's birthday. And this is merely a tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic