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).
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
posted
0
Thanks Joanne for correcting that.. There are some method like getMethod() etc
Bauke Scholtz
Ranch Hand
Joined: Oct 08, 2006
Posts: 2458
posted
0
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 ]
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
posted
0
TO:Campbell Ritchie
public class MyClass { public void print() { System.out.println("MyClass"); } }
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.
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.
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
posted
0
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: 32604
4
posted
0
Originally posted by Usha Pnatha: TO:Campbell Ritchie