• Post Reply Bookmark Topic Watch Topic
  • New Topic

Main function return code  RSS feed

 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
I am getting NoSuchMethodError when my main() function returns int,

I remember in C language it is possible to return an integer to the calling script like so:
int main( int argc, const char* argv[] )[/code]

In Java when my method signature is returning int i get Exception when running the class from command line:
Exception in thread "main" java.lang.NoSuchMethodError: main

Method signature:
public static int main(String args[]){...}

When method signature changed back to void:
public static void main(String args[]){...}
Its working ok.

What am i doing wrong?

 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should be void. That is the main method declaration. If you want your program to have a return code use System.exit()
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main method is introduced here.
 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:The main method is introduced here.

Thank you Christophe,
The documentation on Sun (oracle) site is brief and doesn't cover the return value topic of main method.


When you change the return value of the main method you don't get compile time error/warning (Eclipse)
When I run the method (int signature) with JUNIT test, i can get a value back from main.
How come same functionality won't work when running thew Class from batch / script?
 
Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably because you feed the JUnit the actual method to be called, in this case the main() with an int. However, the JVM looks for a main with a void and doesnt find it.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In some ways, a method called 'main' is no different from any other method. you can overload it, change its return type or its access modifier. you could have a private string main(int a, int b, int c) method, and that is 100% legal. You can call it like any other method (provided you meet the access restrictions, etc).

HOWEVER, when fire up the JVM and say "java MyClass", the JVM is ONLY looking for a method with this exact signiature:

public static void main(String [] args)

If you don't have a method that looks exactly like that, the JVM dies as it can't call the one method it knows should exist.

When you are using your jUnit test, you are explicitly calling the main() method with the different signature (I'm guessing).
 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote: the JVM is ONLY looking for a method with this exact signiature: public static void main(String [] args)


Ok thats the point, the JVM looks for the VOID signature.
So if I am running my class using DOS and the program will end using System.exit(ERRLVL) the ERRORLEVEL will be updated according to ERRLVL right?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's right.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!