{"record":{"id":"77bce69ac4e71019","repo":"apache/cassandra","slug":"could-not-compile-function-s-from-java-source","errorCode":null,"errorMessage":"Could not compile function '%s' from Java source: %s","messagePattern":"Could not compile function '(.+?)' from Java source: (.+?)","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/functions/JavaBasedUDFunction.java","lineNumber":368,"sourceCode":"                }\n\n                if (nonSyntheticMethodCount != 3 || cls.getDeclaredConstructors().length != 1)\n                    throw new InvalidRequestException(\"Check your source to not define additional Java methods or constructors\");\n                MethodType methodType = MethodType.methodType(void.class)\n                                                  .appendParameterTypes(UDFDataType.class, UDFContext.class);\n                MethodHandle ctor = MethodHandles.lookup().findConstructor(cls, methodType);\n                this.javaUDF = (JavaUDF) ctor.invokeWithArguments(resultType, udfContext);\n            }\n            finally\n            {\n                thread.setContextClassLoader(orig);\n            }\n        }\n        catch (InvocationTargetException e)\n        {\n            // in case of an ITE, use the cause\n            logger.error(String.format(\"Could not compile function '%s' from Java source:%n%s\", name, javaSource), e);\n            throw new InvalidRequestException(String.format(\"Could not compile function '%s' from Java source: %s\", name, e.getCause()));\n        }\n        catch (InvalidRequestException | VirtualMachineError e)\n        {\n            throw e;\n        }\n        catch (Throwable e)\n        {\n            logger.error(String.format(\"Could not compile function '%s' from Java source:%n%s\", name, javaSource), e);\n            throw new InvalidRequestException(String.format(\"Could not compile function '%s' from Java source: %s\", name, e));\n        }\n    }\n\n    @Override\n    protected ExecutorService executor()\n    {\n        return executor;\n    }\n","sourceCodeStart":350,"sourceCodeEnd":386,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/functions/JavaBasedUDFunction.java#L350-L386","documentation":"The Java UDF source was compiled but invoking the generated class (e.g. its constructor or a method via reflection) threw an InvocationTargetException. Cassandra unwraps the underlying cause, logs the full Java source, and rethrows as an InvalidRequestException so the user sees the root problem from their code.","triggerScenarios":"CREATE FUNCTION with Java source whose generated class throws during construction or first invocation — e.g. static initializer failure, null/invalid argument to the generated constructor, or an exception thrown eagerly inside the UDF wrapper.","commonSituations":"UDF body references a class not on the classpath causing NoClassDefFoundError; static field initialization throwing; wrong return type causing a MethodHandle invocation failure.","solutions":["Read the nested cause (%s in the message) to identify the real exception from the UDF source","Fix the failing code in the UDF Java source (imports, class references, types)","Verify any classes referenced by the UDF exist in lib/ or the classpath on every node","Re-create the function with corrected source after testing the logic standalone in plain Java"],"exampleFix":"// before (UDF body throws during class init)\nstatic final int LIMIT = Integer.parseInt(System.getProperty(\"udf.limit\"));\nreturn x < LIMIT;\n// after\nreturn x < 100; // avoid env-dependent static init inside UDFs","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { session.execute(createStmt); }\ncatch (InvalidRequestException e) {\n  if (e.getMessage().startsWith(\"Could not compile function\")) {\n    Throwable cause = e.getCause(); // root cause from UDF source\n    log.warn(\"UDF init failed: {}\", cause, cause);\n  } else throw e;\n}","preventionTips":["Never perform environment-dependent work (System properties, network, files) in UDF static initializers","Test the UDF body as plain Java before deploying","Ensure all referenced classes exist on every node's classpath"],"tags":["udf","java","compilation","invocation-target"],"backgroundTag":"udf-compilation-failed","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}