jwtk/jjwt · error · IllegalStateException
Unable to invoke class method ${clazzName}#${methodName}. En
Error message
Unable to invoke class method ${clazzName}#${methodName}. Ensure the necessary implementation is in the runtime classpath. What it means
IllegalStateException thrown by the Class-based io.jsonwebtoken.lang.Classes.invokeStatic when the reflected static method invocation fails. If the underlying cause is a RuntimeException it is rethrown directly; otherwise the message includes the class and method names with a classpath hint.
Source
Thrown at api/src/main/java/io/jsonwebtoken/lang/Classes.java:329
* @param args the actual runtime arguments to use when invoking the method
* @param <T> the type of object expected to be returned from the method
* @return the result returned by the invoked method.
* @since 0.12.0
*/
@SuppressWarnings("unchecked")
public static <T> T invokeStatic(Class<?> clazz, String methodName, Class<?>[] argTypes, Object... args) {
try {
Method method = clazz.getDeclaredMethod(methodName, argTypes);
method.setAccessible(true);
return (T) method.invoke(null, args);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
Throwable cause = e.getCause();
if (cause instanceof RuntimeException) {
throw ((RuntimeException) cause); //propagate
}
String msg = "Unable to invoke class method " + clazz.getName() + "#" + methodName +
". Ensure the necessary implementation is in the runtime classpath.";
throw new IllegalStateException(msg, e);
}
}
/**
* Returns the {@code instance}'s named (declared) field value.
*
* @param instance the instance with the internal field
* @param fieldName the name of the field to inspect
* @param fieldType the type of field to inspect
* @param <T> field instance value type
* @return the field value
*/
public static <T> T getFieldValue(Object instance, String fieldName, Class<T> fieldType) {
if (instance == null) return null;
try {
Field field = instance.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
Object o = field.get(instance);View on GitHub (pinned to fb71496164)
Solutions
- Inspect the cause to see whether the target method threw or resolution failed
- Correct the argTypes/args to match the exact static method signature
- Ensure the classpath holds the intended jar version with that method
- Catch the propagated RuntimeException separately, as it is rethrown unwrapped
Example fix
// before
Object r = Classes.invokeStatic(Impl.class, "create", new Class[]{String.class}, new Object[]{42}); // wrong arg type
// after
Object r = Classes.invokeStatic(Impl.class, "create", new Class[]{Integer.class}, new Object[]{42}); Defensive patterns
Strategy: try-catch
Validate before calling
try {
clazz.getMethod(methodName, argTypes);
} catch (NoSuchMethodException e) {
throw new IllegalArgumentException("No static method " + methodName + " with those arg types on " + clazz);
} Try / catch
try {
return Classes.invokeStatic(clazz, methodName, argTypes, args);
} catch (IllegalStateException e) {
// non-runtime exceptions land here wrapped; RuntimeExceptions are rethrown directly by the library
throw e;
} catch (RuntimeException e) {
// method's own runtime exception propagated unwrapped
throw e;
} Prevention
- Verify argTypes exactly match the declared parameter types
- Pin library versions so method signatures do not drift
- Handle the target method's runtime exceptions separately since they propagate unwrapped
When it happens
Trigger: invokeStatic(clazz, methodName, argTypes, args) where the method throws a checked/non-runtime exception, or method resolution fails after the Class was loaded (e.g. NoSuchMethodException due to argTypes mismatch).
Common situations: Static method's internal code throwing a checked exception; passing argTypes that don't match the declared method; class loaded but from a jar version where the method signature changed.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Unable to invoke class method ${fqcn}#${methodName}. Ensure
- Unable to load class named [${fqcn}] from the thread context
- Class method parameter cannot be null.
- Unable to instantiate class [${clazzName}]
- Unable to instantiate instance with constructor [${ctor}]
AI-assisted analysis of jwtk/jjwt@fb71496164 (2026-09-09).
Data as JSON: /api/errors/dab5b7c51c843250.
Report an issue: GitHub.