{"record":{"id":"6c4aa9b6818cdff5","repo":"apache/pulsar","slug":"class-must-have-a-no-arg-constructor","errorCode":null,"errorMessage":"Class  must have a no-arg constructor","messagePattern":"Class  must have a no-arg constructor","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"pulsar-functions/runtime-all/src/main/java/org/apache/pulsar/functions/instance/JavaInstanceMain.java","lineNumber":150,"sourceCode":"                                        ClassLoader classLoader) {\n        Class<?> theCls;\n        try {\n            theCls = Class.forName(userClassName, true, classLoader);\n        } catch (ClassNotFoundException | NoClassDefFoundError cnfe) {\n            throw new RuntimeException(\"Class \" + userClassName + \" must be in class path\", cnfe);\n        }\n        Object result;\n        try {\n            Constructor<?> meth = theCls.getDeclaredConstructor();\n            meth.setAccessible(true);\n\n            result = meth.newInstance();\n        } catch (InstantiationException ie) {\n            throw new RuntimeException(\"User class must be concrete\", ie);\n        } catch (NoSuchMethodException e) {\n            throw new RuntimeException(\"Class \" + userClassName + \" doesn't have such method\", e);\n        } catch (IllegalAccessException e) {\n            throw new RuntimeException(\"Class \" + userClassName + \" must have a no-arg constructor\", e);\n        } catch (InvocationTargetException e) {\n            throw new RuntimeException(\"Class \" + userClassName + \" constructor throws exception\", e);\n        }\n        return result;\n    }\n\n    public static ClassLoader loadJar(ClassLoader parent, File[] jars) throws MalformedURLException {\n        URL[] urls = new URL[jars.length];\n        for (int i = 0; i < jars.length; i++) {\n            urls[i] = jars[i].toURI().toURL();\n        }\n        return new URLClassLoader(urls, parent);\n    }\n\n    public static boolean isBlank(String str) {\n        int strLen;\n        if (str != null && (strLen = str.length()) != 0) {\n            for (int i = 0; i < strLen; ++i) {","sourceCodeStart":132,"sourceCodeEnd":168,"githubUrl":"https://github.com/apache/pulsar/blob/820761864ed8e2a7d2e52dd9763ad2ae117c1395/pulsar-functions/runtime-all/src/main/java/org/apache/pulsar/functions/instance/JavaInstanceMain.java#L132-L168","documentation":"createInstance() calls setAccessible(true) on the no-arg constructor and then newInstance(). IllegalAccessException means the constructor is not accessible from the caller (e.g. package-private/private class or constructor, or sealed module), and Pulsar rethrows as RuntimeException(\"Class X must have a no-arg constructor\").","triggerScenarios":"Function class or its no-arg constructor declared private/package-private; class itself is package-private (not public); Java module/SecurityManager blocking setAccessible.","commonSituations":"Defining the function as a nested or package-private class; making the constructor private to enforce single instantiation; JPMS strong encapsulation in newer JDKs.","solutions":["Declare the function class public and give it a public no-arg constructor.","Move inner/nested function classes to top-level public classes.","If running on a modular JDK, open the function package (--add-opens) or avoid modules for the function jar.","Ensure no SecurityManager policy denies ReflectPermission/accessDeclaredMembers."],"exampleFix":"// before\nclass MyFunction implements Function<String, Integer> { ... }\n// after\npublic class MyFunction implements Function<String, Integer> { public MyFunction() {} ... }","handlingStrategy":"validation","validationCode":"Class<?> c = Class.forName(className);\nif (!java.lang.reflect.Modifier.isPublic(c.getModifiers())) throw new IllegalArgumentException(\"Function class must be public\");\njava.lang.reflect.Constructor<?> k = c.getDeclaredConstructor();\nk.setAccessible(true); // will throw here in your CI before deployment if inaccessible","typeGuard":"boolean publiclyInstantiable(String fcn) { try { Class<?> c = Class.forName(fcn); return java.lang.reflect.Modifier.isPublic(c.getModifiers()) && java.lang.reflect.Modifier.isPublic(c.getDeclaredConstructor().getModifiers()); } catch (Throwable t) { return false; } }","tryCatchPattern":"try { Object f = JavaInstanceMain.createInstance(className, cl); } catch (RuntimeException e) { if (e.getMessage().contains(\"no-arg constructor\")) { System.err.println(\"Make \" + className + \" and its constructor public\"); } throw e; }","preventionTips":["Declare function classes and no-arg constructors public.","Avoid package-private/nested function classes.","Avoid Java modules for function jars, or add appropriate --add-opens.","Never make the constructor private to 'enforce' usage patterns in function classes."],"tags":["reflection","access","constructor","function-config"],"backgroundTag":"illegal-access","analyzedSha":"820761864ed8e2a7d2e52dd9763ad2ae117c1395","analyzedAt":"2026-09-06T00:14:20.138Z","contentChangedAt":"2026-09-06T00:14:20.138Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}