{"record":{"id":"c9a0b614bcaa3406","repo":"karatelabs/karate","slug":"failed-to-instantiate-classname","errorCode":null,"errorMessage":"Failed to instantiate <className>","messagePattern":"Failed to instantiate <className>","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/Runner.java","lineNumber":687,"sourceCode":"         */\n        public Builder listenerFactory(String className) {\n            if (className != null && !className.isEmpty()) {\n                try {\n                    Class<?> clazz = Class.forName(className);\n                    Object instance = clazz.getDeclaredConstructor().newInstance();\n                    if (instance instanceof RunListenerFactory factory) {\n                        listenerFactories.add(factory);\n                    } else if (instance instanceof RunListener listener) {\n                        // If it's a RunListener, wrap it in a factory that returns the same instance\n                        listeners.add(listener);\n                    } else {\n                        throw new IllegalArgumentException(\n                                \"Class \" + className + \" must implement RunListenerFactory or RunListener\");\n                    }\n                } catch (ClassNotFoundException e) {\n                    throw new IllegalArgumentException(\"Class not found: \" + className, e);\n                } catch (Exception e) {\n                    throw new IllegalArgumentException(\"Failed to instantiate \" + className, e);\n                }\n            }\n            return this;\n        }\n\n        /**\n         * Add a result listener for streaming test results.\n         */\n        public Builder resultListener(ResultListener listener) {\n            if (listener != null) {\n                resultListeners.add(listener);\n            }\n            return this;\n        }\n\n        /**\n         * Add multiple result listeners.\n         */","sourceCodeStart":669,"sourceCodeEnd":705,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/Runner.java#L669-L705","documentation":"After successfully loading the listener class, listenerFactory attempts to instantiate it via a no-arg constructor; any failure (instantiation/access exception) is wrapped in 'Failed to instantiate <className>'. The class exists but cannot be constructed in this context.","triggerScenarios":"Thrown at karate-core/src/main/java/io/karatelabs/core/Runner.java:687 when the library encounters an invalid state.","commonSituations":"Listener has no public no-arg constructor (only parameterized ones); class is abstract or an interface; constructor throws an exception during init (e.g. reading missing config); inner class without the outer instance; constructor not public.","solutions":["Add a public no-arg constructor to the listener class","Check the constructor body for exceptions (read the wrapped cause) — e.g. missing files or config it reads at init","Make the class concrete (not abstract/interface) and public","If it's a non-static inner class, make it static or top-level"],"exampleFix":"// before\npublic class MyListener {\n    public MyListener(Path cfg) { ... }\n}\n// after\npublic class MyListener {\n    public MyListener() { this(defaultCfg()); }\n    public MyListener(Path cfg) { ... }\n}","handlingStrategy":"try-catch","validationCode":"Class<?> c = Class.forName(name);\nif (Modifier.isAbstract(c.getModifiers()) || c.isInterface() || c.getConstructors().length == 0)\n    throw new IllegalStateException(\"listener lacks a public constructor: \" + name);\nc.getDeclaredConstructor().newInstance(); // dry-run check","typeGuard":"boolean instantiable(Class<?> c) { try { c.getDeclaredConstructor().newInstance(); return true; } catch (ReflectiveOperationException e) { return false; } }","tryCatchPattern":"try { builder.listener(name); } catch (IllegalArgumentException e) { if (e.getMessage().startsWith(\"Failed to instantiate\")) { log.error(\"{} needs a public no-arg constructor; cause: {}\", name, e.getCause()); } throw e; }","preventionTips":["Give every listener a public no-arg constructor; load config lazily, not in the constructor","Never make listeners abstract or non-static inner classes","Dry-run newInstance() in a unit test for each registered listener"],"tags":["runner","listeners","reflection","constructor"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}