jwtk/jjwt · error · InstantiationException

Unable to instantiate instance with constructor [${ctor}]

Error message

Unable to instantiate instance with constructor [${ctor}]

What it means

InstantiationException thrown by io.jsonwebtoken.lang.Classes.instantiate(Constructor, Object...) when Constructor.newInstance(args) fails. Any exception from the constructor (access, argument mismatch, or constructor-internal failure) is wrapped with a message naming the constructor. Called by Classes.newInstance(Class, Object...).

Source

Thrown at api/src/main/java/io/jsonwebtoken/lang/Classes.java:277

    }

    /**
     * Creates a new object using the specified {@link Constructor}, invoking it with the specified constructor
     * {@code args} arguments.
     *
     * @param ctor the constructor to invoke
     * @param args the arguments to supply to the constructor
     * @param <T>  the type of object to create
     * @return the new object instance
     * @throws InstantiationException if the constructor cannot be invoked successfully
     */
    public static <T> T instantiate(Constructor<T> ctor, Object... args) {
        try {
            return ctor.newInstance(args);
        } catch (Exception e) {
            String msg = "Unable to instantiate instance with constructor [" + ctor + "]";
            throw new InstantiationException(msg, e);
        }
    }

    /**
     * Invokes the fully qualified class name's method named {@code methodName} with parameters of type {@code argTypes}
     * using the {@code args} as the method arguments.
     *
     * @param fqcn       fully qualified class name to locate
     * @param methodName name of the method to invoke on the class
     * @param argTypes   the method argument types supported by the {@code methodName} method
     * @param args       the runtime arguments to use when invoking the located class method
     * @param <T>        the expected type of the object returned from the invoked method.
     * @return the result returned by the invoked method
     * @since 0.10.0
     */
    public static <T> T invokeStatic(String fqcn, String methodName, Class<?>[] argTypes, Object... args) {
        try {
            Class<?> clazz = Classes.forName(fqcn);

View on GitHub (pinned to fb71496164)

Solutions

  1. Inspect the cause (getCause) for the real constructor failure
  2. Match the argument list exactly to the constructor signature the library selects
  3. Make the constructor public and, for JDK 9+, open the relevant package (--add-opens or module exports)
  4. Fix validation logic inside the target constructor that is throwing

Example fix

// before
JwtParser p = Classes.newInstance(ParserClass.class, wrongArg); // arg type mismatch
// after
JwtParser p = Classes.newInstance(ParserClass.class, expectedTypedArg);
Defensive patterns

Strategy: try-catch

Validate before calling

for (Constructor<?> ct : clazz.getConstructors()) {
    if (Arrays.equals(ct.getParameterTypes(), argTypes)) return; // signature OK
}
throw new IllegalArgumentException("No matching constructor on " + clazz);

Try / catch

try {
    return Classes.newInstance(clazz, args);
} catch (InstantiationException e) {
    Throwable root = e.getCause();
    throw new IllegalStateException("Constructor failed: " + (root != null ? root.getMessage() : e.getMessage()), e);
}

Prevention

When it happens

Trigger: Calling Classes.newInstance(clazz, args) where the resolved constructor throws (including InvocationTargetException wrapping a constructor-internal exception), the args do not match the constructor signature, or the constructor is inaccessible.

Common situations: Passing wrong argument types/count for the configured class's constructor; constructor validation throwing (e.g. null key); reflective instantiation of classes with package-private constructors; JDK 9+ module access restrictions.

Related errors


AI-assisted analysis of jwtk/jjwt@fb71496164 (2026-09-09). Data as JSON: /api/errors/abcb1fe3e89cc2c0. Report an issue: GitHub.