{"record":{"id":"59b78b7a7c4cd540","repo":"apache/beam","slug":"unable-to-find-usercodeexception-wrap","errorCode":null,"errorMessage":"Unable to find UserCodeException.wrap","messagePattern":"Unable to find UserCodeException\\.wrap","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/reflect/ByteBuddyDoFnInvokerFactory.java","lineNumber":1457,"sourceCode":"        @Nullable Integer returnVarIndex,\n        MethodDescription targetMethod,\n        MethodDescription instrumentedMethod) {\n      this.returnVarIndex = returnVarIndex;\n      this.targetMethod = targetMethod;\n      this.instrumentedMethod = instrumentedMethod;\n      this.returnType = targetMethod.getReturnType().asErasure();\n\n      boolean targetMethodReturnsVoid = TypeDescription.VOID.equals(returnType);\n      checkArgument(\n          (returnVarIndex == null) == targetMethodReturnsVoid,\n          \"returnVarIndex should be defined if and only if the target method has a return value\");\n\n      try {\n        createUserCodeException =\n            new MethodDescription.ForLoadedMethod(\n                UserCodeException.class.getDeclaredMethod(\"wrap\", Throwable.class));\n      } catch (NoSuchMethodException | SecurityException e) {\n        throw new RuntimeException(\"Unable to find UserCodeException.wrap\", e);\n      }\n    }\n\n    @Override\n    public boolean isValid() {\n      return true;\n    }\n\n    private Object describeType(Type type) {\n      switch (type.getSort()) {\n        case Type.OBJECT:\n          return type.getInternalName();\n        case Type.INT:\n        case Type.BYTE:\n        case Type.BOOLEAN:\n        case Type.SHORT:\n          return Opcodes.INTEGER;\n        case Type.LONG:","sourceCodeStart":1439,"sourceCodeEnd":1475,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/reflect/ByteBuddyDoFnInvokerFactory.java#L1439-L1475","documentation":"ByteBuddyDoFnInvokerFactory looks up UserCodeException.wrap(Throwable) via getDeclaredMethod to embed user-code exception wrapping into generated DoFn invoker classes. If the method cannot be resolved (missing, renamed, or inaccessible under the active SecurityManager), a RuntimeException with this message is thrown. It signals an inconsistent Beam runtime or a restrictive security policy rather than a user-code bug.","triggerScenarios":"Loading ByteBuddyDoFnInvokerFactory (e.g. creating a DoFnInvoker via DoFnInvoker.newInvoker) when UserCodeException has no declared `wrap(Throwable)` method, or getDeclaredMethod throws SecurityException under an active SecurityManager that denies reflect access to org.apache.beam classes.","commonSituations":"Shaded/relocated copies of UserCodeException differing from the invoker factory; partial classpath with pruned Beam classes (ProGuard/R8 strip); sandboxed environments (application servers, Flink/Spark security profiles) with a SecurityManager blocking reflection on Beam internals.","solutions":["Ensure a single consistent beam-sdks-java-core version on the classpath; remove duplicate/stale Beam jars.","Verify UserCodeException.class.getDeclaredMethods() contains wrap(Throwable); if not, rebuild/re-extract the jar.","Grant reflection permissions for org.apache.beam.* in the java.security.policy file, or disable the restrictive SecurityManager.","Fix shading config so the invoker factory and UserCodeException are relocated together (same prefix) or not relocated at all."],"exampleFix":"// before: policy denies reflection\n// grant { /* nothing for org.apache.beam */ };\n// after (java.policy):\ngrant codeBase \"file:/path/to/beam-sdks-java-core-2.60.0.jar\" {\n  permission java.lang.RuntimePermission \"accessDeclaredMembers\";\n};","handlingStrategy":"validation","validationCode":"try {\n  org.apache.beam.sdk.transforms.reflect.UserCodeException.class\n      .getDeclaredMethod(\"wrap\", Throwable.class);\n} catch (NoSuchMethodException | SecurityException e) {\n  throw new IllegalStateException(\"UserCodeException.wrap unavailable; check Beam jars/security policy\", e);\n}","typeGuard":null,"tryCatchPattern":"try {\n  DoFnInvoker invoker = DoFnInvoker.newInvoker(myDoFn);\n} catch (RuntimeException e) {\n  if (e.getMessage() != null && e.getMessage().contains(\"Unable to find UserCodeException.wrap\")) {\n    log.error(\"Beam jar mismatch or reflection denied; verify single beam-sdks-java-core version and policy grants\", e);\n    throw e;\n  }\n  throw e;\n}","preventionTips":["Pin all org.apache.beam artifacts to one version","Review java.security.policy for reflection permissions when a SecurityManager is active","Verify shading configs relocate the whole Beam package tree consistently","Smoke-test DoFn invoker creation in CI under the same security configuration as production"],"tags":["java","reflection","classpath","security","apache-beam"],"backgroundTag":"class-not-found","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}