I can't find exact difference between NoSuchMethodException
and NoSuchMethodError
in Java. Could someone give explanation and example of these two things ?

- 85
- 1
- 6

- 645
- 3
- 9
- 16
-
1NoSuchMethodException occurs when you call a method where you might expect the target method might be missing. NoSuchMethodError occurs when the method is missing but normally it wouldn't be i.e. in normal code. – Peter Lawrey Jan 14 '15 at 08:51
4 Answers
NoSuchMethodException can be thrown when you're invoking a method through reflection, and the name of the method comes from a variable in your program.
NoSuchMethodError can be thrown when a compiled Java class does a regular method call to another class and the method doesn't exist. (This usually happens when the caller class was compiled against one version of the class being called, and is being executed together with another version of that class, which no longer has the method.)

- 92,896
- 20
- 260
- 197
-
About the `NoSuchMethodException` case, does the name of the method actually need to come from a variable for this exception to be thrown ? – SantiBailors Feb 17 '17 at 10:49
-
Well, it could be a field, or the result of calling a method or evaluating an expression. – yole Feb 17 '17 at 12:02
-
Sure, actually my question wasn't too clear, I meant can it be a string literal or constant ? – SantiBailors Feb 17 '17 at 13:19
NoSuchMethodException
occurs when you try to invoke a method using reflection.
NoSuchMethodError
occurs when you had that method while compiling but dont have it during the runtime.
Consider the following example for NoSuchMethodError
Class : Person.java
public class Person{
public String getName(){
return "MyName";
}
}
Compile it using
javac Person.java
And now try to run this using
java Person
It ll give you
java.lang.NoSuchMethodError: main
Exception in thread "main"
Because it tries to find the public static void main(String [] args)
which is not there
For NoSuchMethodException
c = Class.forName("java.lang.String");
try
{
Class[] paramTypes = new Class[2];
Method m = c.getDeclaredMethod("myMethod", paramTypes);
}
this is ll throw an exception saying
java.lang.NoSuchMethodException: java.lang.String.myMethod(null, null)
Consider this link which has a better explaination

- 2,278
- 11
- 23
NoSuchMethodException
is thrown when you try and get a method that doesn't exist with reflection. E.g. by calling Class#getDeclaredMethod(name, parameters)
with either the wrong name or parameters.
NoSuchMethodError
is thrown when the virtual machine cannot find the method you are trying to call. This can happen when you compile with one version of a library, and later run the application with another version of the library on the classpath (e.g. an older one that doesn't have the method you're calling)

- 2,871
- 9
- 33
- 36

- 524
- 2
- 12
Thrown when a particular method cannot be found.
Thrown if an application tries to call a specified method of a class (either static or instance), and that class no longer has a definition of that method.
Also see this article, it explains it better.

- 94,125
- 30
- 188
- 241
-
Hai Maroun : I can't understand "class no longer has a definition of that method" Please explain this statement. – Balasubramani Jan 14 '15 at 08:47
-
4This means that the calling class is not aware of the changed class. The method was there when the calling class was compiled but while the runtime - something changed and its not able to find it anymore – shikjohari Jan 14 '15 at 08:49
-
4@Balasubramani You have a method A.method1() which calls B.method2(), you remove B.method2() without recompiling class A. When A.method1() is run you get a NoSuchMethodError – Peter Lawrey Jan 14 '15 at 08:53
-
-
The linked page says _Since [NoSuchMethodException] is a Exception you can simply catch it and can proceed with your flow, where as it is not possible with NoSuchMethodError_ Any idea why it says it's not possible ? It might not be advisable to catch `Error`s, or something else as subjective as that, but it's definitely possible, and it should be possible for any `Throwable`. – SantiBailors Feb 17 '17 at 11:06
-
@SantiBailors The Error class and subclasses, including NoSuchMethodError, have no good way to handle other than logging/reporting at the top level. Errors are unexpected conditions that the programmer is not expected to handle. Also globally catching Error or Throwable fucks with certain functionality (namely threads stopping each other -- the stop works via an Error being injected into the target thread) – Paul Stelian Feb 14 '22 at 17:02