Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes NoSuchMethodException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "NoSuchMethodException" Watch "NoSuchMethodException" New topic
Author

NoSuchMethodException

Usha Pnatha
Greenhorn

Joined: Aug 01, 2008
Posts: 27
Hello Ranchers,

I want to know why these exceptions arise in a Java program:
ClassNotFoundException, NoSuchMethodException

How these exceptions can occur even there are no compilation errors?

I found some reasons for the ClassNotFoundException in the API.
But I've no clue about NoSuchMethodException.

Can anyone please share your knowledge about this?

Also if there are any resources which describe about the cause for all popular exceptions, please share it with me.

Thanks in advance.

With Regards,
Usha
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
javac MyClass.java
java myclass
java MyClass


That should show you how it in action!
Sunil Kumar
Ranch Hand

Joined: Apr 24, 2007
Posts: 76
ClassNotFoundException is generally thrown in case when you use Class.forName("NonExistingClass") or in applications used to map methods/classes with xml or properties like hibernate / struts etc ..

Or if you decide to push your code compiled on some jar/class base and run it on different jar/class base (lacking some classes and methods).


Sunil Kumar
http://goodtoknowit.blogspot.com/
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
Several methods in the Class class can also throw a NoSuchMethodException.
[ December 16, 2008: Message edited by: Joanne Neal ]

Joanne
Sunil Kumar
Ranch Hand

Joined: Apr 24, 2007
Posts: 76
Thanks Joanne for correcting that.. There are some method like getMethod() etc
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Simply because the classpath used during compile is not necessarily the same as the classpath used during runtime.

NoClassDefFoundError denotes a missing class in the classpath during runtime, while it was available in the classpath during compile. The solution is usually to put the desired class in the classpath.

NoSuchMethodException denotes a missing method associated with a class in the classpath during runtime, while it was available in the classpath during compile. The solution is usually to upgrade the class to a newer version containing that method, or to remove duplicate and older versioned class file from the classpath.
[ December 16, 2008: Message edited by: Bauke Scholtz ]
Usha Pnatha
Greenhorn

Joined: Aug 01, 2008
Posts: 27
Thank you very much Campbell Ritchie ,Sunil Kumar ,Joanne Neal for your explanations.

Is there any possibility for the "NoSuchMethodException" other than accessing the methods of the "Class" class.
I mean is there any possibility for a NoSuchMethodException in the case of a program using polymorphism.

Actually, this original question was raised by my lecturer in today's class & he didn't give the answer. He asked us to think in the context of polymorphism and answer the question. But we couldn't.

So, can anyone please help me in finding the answer.


Thank you.
Usha Pnatha
Greenhorn

Joined: Aug 01, 2008
Posts: 27
TO:Campbell Ritchie



public class MyClass
{
public void print()
{
System.out.println("MyClass");
}
}

javac MyClass.java
java myclass
java MyClass


That should show you how it in action!


I tried with this. But I'm getting an java.lang.NoClassDefFoundError.
Can you explain how it happens??

Thanks in advance...
Sunil Kumar
Ranch Hand

Joined: Apr 24, 2007
Posts: 76
Yes, that is really a very good topic to talk about.
if we define a typless(of type Object) variable . When a method is invoked, the runtime determines if the method is implemented by the receiving object or not. If it is there, it calls the method otherwise it throws NoSuchMethodException.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Originally posted by Usha Pnatha:

I tried with this. But I'm getting an java.lang.NoClassDefFoundError.
Can you explain how it happens??

Thanks in advance...

Java is case sensitive.

Have you noticed my previous reply anyway?
[ December 16, 2008: Message edited by: Bauke Scholtz ]
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

Consider String#split() which was added in 1.4.
If you compile your code with 1.4+ but, try to run it on say 1.3, you will get a NoSuchMethodException.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Originally posted by Sunil Kumar:
Yes, that is really a very good topic to talk about.
if we define a typless(of type Object) variable . When a method is invoked, the runtime determines if the method is implemented by the receiving object or not. If it is there, it calls the method otherwise it throws NoSuchMethodException.

The topicstarter wasn't wondering how they could occur, but why they only occur during runtime and not during compile. The answer is already given: the classpath during compile is not necessarily the same as the classpath during runtime.
Sunil Kumar
Ranch Hand

Joined: Apr 24, 2007
Posts: 76
Hey Bauke, all exceptions occurs at runtime only. Further my point was the interpretation of which method is to be called is done at runtime, while at compile time it uses the references and compiler throws no error when it sees that all references have the so called method
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Originally posted by Usha Pnatha:
TO:Campbell Ritchie

But I'm getting an java.lang.NoClassDefFoundError.


Wrong %$@** exception! Sorry.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NoSuchMethodException