{"record":{"id":"c2d9f38b0d531c02","repo":"jwtk/jjwt","slug":"unable-to-instantiate-class-clazzname","errorCode":null,"errorMessage":"Unable to instantiate class [${clazzName}]","messagePattern":"Unable to instantiate class \\[(.+?)\\]","errorType":"exception","errorClass":"InstantiationException","httpStatus":null,"severity":"error","filePath":"api/src/main/java/io/jsonwebtoken/lang/Classes.java","lineNumber":221,"sourceCode":"        return (T) newInstance(forName(fqcn), args);\n    }\n\n    /**\n     * Creates a new instance of the specified {@code clazz} via {@code clazz.newInstance()}.\n     *\n     * @param clazz the class to invoke\n     * @param <T>   the type of the object created\n     * @return the newly created object\n     */\n    public static <T> T newInstance(Class<T> clazz) {\n        if (clazz == null) {\n            String msg = \"Class method parameter cannot be null.\";\n            throw new IllegalArgumentException(msg);\n        }\n        try {\n            return clazz.newInstance();\n        } catch (Exception e) {\n            throw new InstantiationException(\"Unable to instantiate class [\" + clazz.getName() + \"]\", e);\n        }\n    }\n\n    /**\n     * Returns a new instance of the specified {@code clazz}, invoking the associated constructor with the specified\n     * {@code args} arguments.\n     *\n     * @param clazz the class to invoke\n     * @param args  the arguments matching an associated class constructor\n     * @param <T>   the type of the created object\n     * @return the newly created object\n     */\n    public static <T> T newInstance(Class<T> clazz, Object... args) {\n        Class<?>[] argTypes = new Class[args.length];\n        for (int i = 0; i < args.length; i++) {\n            argTypes[i] = args[i].getClass();\n        }\n        Constructor<T> ctor = getConstructor(clazz, argTypes);","sourceCodeStart":203,"sourceCodeEnd":239,"githubUrl":"https://github.com/jwtk/jjwt/blob/fb71496164c71442d08adec4571d9616ed5e1b8d/api/src/main/java/io/jsonwebtoken/lang/Classes.java#L203-L239","documentation":"InstantiationException thrown by io.jsonwebtoken.lang.Classes.newInstance(Class) when clazz.newInstance() fails for any reason (no public no-arg constructor, abstract class/interface, constructor throws, or access restrictions). The original exception is attached as the cause.","triggerScenarios":"Calling Classes.newInstance on an interface or abstract class; the class has no public no-arg constructor; the no-arg constructor itself throws; the class is not accessible from the caller's classloader/package.","commonSituations":"Configuring a custom class in jjwt config that is an interface or lacks a no-arg constructor; inner classes without static modifier (no implicit no-arg ctor on instances); JDK module/access restrictions (e.g. JDK 9+ strong encapsulation) blocking instantiation.","solutions":["Ensure the target class is public, concrete (not abstract/interface), and has a public no-arg constructor","Inspect the cause exception (InstantiationException/InvocationTargetException) for the real failure","If the constructor throws, fix the throwing initialization code inside that constructor","Make the class static if it is a non-static inner class, or add an explicit public no-arg constructor"],"exampleFix":"// before\npublic class MySigner implements Signer { // interface instantiation fails if MySigner were abstract\n    public MySigner(String key) { ... } // no no-arg ctor\n}\n// after\npublic class MySigner implements Signer {\n    public MySigner() { ... } // public no-arg constructor\n}","handlingStrategy":"try-catch","validationCode":"Class<?> c = ...;\nif (c.isInterface() || Modifier.isAbstract(c.getModifiers())) {\n    throw new IllegalArgumentException(c + \" is not instantiable\");\n}\ntry { c.getDeclaredConstructor(); } catch (NoSuchMethodException e) {\n    throw new IllegalArgumentException(c + \" lacks a public no-arg constructor\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    return Classes.newInstance(clazz);\n} catch (InstantiationException e) {\n    LOG.error(\"Cannot instantiate \" + clazz + \": \" + e.getCause(), e);\n    throw e;\n}","preventionTips":["Ensure custom classes are public, concrete, and have a public no-arg constructor","Make inner classes static if instantiated reflectively","Test reflective instantiation of configured classes at startup"],"tags":["reflection","instantiation","constructor"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"fb71496164c71442d08adec4571d9616ed5e1b8d","analyzedAt":"2026-09-09T00:33:09.982Z","contentChangedAt":"2026-09-09T00:33:09.982Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}