Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NoSuchMethodException

 
Usha Pnatha
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
javac MyClass.java
java myclass
java MyClass


That should show you how it in action!
 
Sunil Kumar
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Several methods in the Class class can also throw a NoSuchMethodException.
[ December 16, 2008: Message edited by: Joanne Neal ]
 
Sunil Kumar
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Joanne for correcting that.. There are some method like getMethod() etc
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11196
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Usha Pnatha:
TO:Campbell Ritchie

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


Wrong %$@** exception! Sorry.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic