This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

When does a NoSuchMethodError occur for an incorrect main method?

 
Ranch Hand
Posts: 113
7
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

From one of Enthuware's mock exams:

What will the following code print when run?


According to the explanation, it'll throw a NoSuchMethodError since this main method returns long rather than void. The error, I'm getting, however, is as follows:

This does happen at run time though. The code compiles without problems.

Is this error a NoSuchMethodError? If it is, why would the error name be omitted?

In the explanation of another Enthuware question, it says:

You can have a main method that doesn't take String[] as an argument. It will not make the class executable from the command line though.


It seems to me that this is what's happening here. I can't execute it from the command line. Makes sense. But that doesn't explain why the first example, according to the explanation, throws a NoSuchMethodError (which it doesn't when I compile and run).

Thanks again!

Regards,
Shane
 
author & internet detective
Posts: 40035
809
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shane,
This behavior changed over time. As you can see, Java 6 gives the behavior mentioned by Enthuware and Java 7 gives the behavior you saw. (As does Java 8).





The good news is that the exam will ask you if the code fails to compile or if it throws an exception at runtime. It won't ask you which one.
 
Rebecca Wolf
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeanne,

Thanks for the clarification. I think it'd be good if Enthuware would update these questions.
 
Rebecca Wolf
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PS. That request wasn't meant for you, obviously. I'll send Paul a message.
 
Enthuware Software Support
Posts: 4450
41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The option indeed only says, "It will throw an Error at runtime.", without mentioning the kind, which is correct.
The explanation has been updated.

thank you for your feedback!
Paul.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just for completeness: the changed behavior only applies to the main(String[]) method. So if you try to invoke a non-existing method, you still can get a NoSuchMethodError at runtime.

If you wonder how you can invoke a non-existing method without getting a compiler error, here is an example. Assume the following classesBoth classes can be successfully compiled and if you run TestGreetings, the output will be (obviously) hi.

Now make a change to the signature of the hi() method (e.g. rename to sayHi) and only compile the Greetings class. If you now run TestGreetings, you'll get the following output:
Exception in thread "main" java.lang.NoSuchMethodError: Greetings.hi()V
       at TestGreetings.main(TestGreetings.java:3)


Please note that although the return type is not used in the TestGreetings class, a change of the return type of the hi() method (and of course adding the appropriate return statement) will also result in a NoSuchMethodError when you run TestGreetings.

Hope it helps!
Kind regards,
Roel
 
Rebecca Wolf
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the addition, Roel.
 
    Bookmark Topic Watch Topic
  • New Topic