{"record":{"id":"e7d82e1d81f0397a","repo":"apache/beam","slug":"error-binding-classes-to-a-jaxb-context","errorCode":null,"errorMessage":"Error binding classes to a JAXB Context.","messagePattern":"Error binding classes to a JAXB Context\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"sdks/java/io/xml/src/main/java/org/apache/beam/sdk/io/xml/XmlIO.java","lineNumber":529,"sourceCode":"    public Write<T> withRootElement(String rootElement) {\n      return toBuilder().setRootElement(rootElement).build();\n    }\n\n    /** Sets the charset used to write the file. */\n    public Write<T> withCharset(Charset charset) {\n      return toBuilder().setCharset(charset.name()).build();\n    }\n\n    @Override\n    public PDone expand(PCollection<T> input) {\n      checkArgument(getRecordClass() != null, \"withRecordClass() is required\");\n      checkArgument(getRootElement() != null, \"withRootElement() is required\");\n      checkArgument(getFilenamePrefix() != null, \"to() is required\");\n      checkArgument(getCharset() != null, \"withCharset() is required\");\n      try {\n        JAXBContext.newInstance(getRecordClass());\n      } catch (JAXBException e) {\n        throw new RuntimeException(\"Error binding classes to a JAXB Context.\", e);\n      }\n\n      ResourceId prefix =\n          FileSystems.matchNewResource(getFilenamePrefix(), false /* isDirectory */);\n      input.apply(\n          FileIO.<T>write()\n              .via(\n                  sink(getRecordClass())\n                      .withCharset(Charset.forName(getCharset()))\n                      .withRootElement(getRootElement()))\n              .to(prefix.getCurrentDirectory().toString())\n              .withPrefix(prefix.getFilename())\n              .withSuffix(\".xml\")\n              .withIgnoreWindowing());\n      return PDone.in(input.getPipeline());\n    }\n\n    @Override","sourceCodeStart":511,"sourceCodeEnd":547,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/io/xml/src/main/java/org/apache/beam/sdk/io/xml/XmlIO.java#L511-L547","documentation":"XmlIO's expand() validates at pipeline-construction time that the configured record class can be bound to a JAXBContext by calling JAXBContext.newInstance(getRecordClass()). If the class is not a valid JAXB type (missing annotations, invalid mappings), the JAXBException is rethrown as this RuntimeException, failing pipeline construction early instead of failing later during execution.","triggerScenarios":"Calling XmlIO.<T>write()/read().withRecordClass(SomeClass.class).expand(input) where SomeClass cannot be bound by JAXB (e.g. no @XmlRootElement or unsupported field types).","commonSituations":"Users passing plain POJOs without JAXB annotations; switching record classes after upgrading JAXB versions; typos resulting in the wrong record class.","solutions":["Annotate the record class correctly (@XmlRootElement, @XmlAccessorType) so JAXB can bind it.","Run JAXBContext.newInstance(recordClass) locally to see the underlying JAXBException detail.","Replace unsupported field types in the record class with JAXB-compatible types.","Ensure a JAXB implementation is on the runtime classpath on Java 11+."],"exampleFix":"// before\npublic class Record { public String name; }\n// after\n@XmlRootElement(name = \"record\")\n@XmlAccessorType(XmlAccessType.FIELD)\npublic class Record { public String name; }","handlingStrategy":"validation","validationCode":"try { JAXBContext.newInstance(Record.class); } catch (JAXBException e) { throw new IllegalArgumentException(\"Record class cannot be bound to JAXB: \" + Record.class, e); }","typeGuard":"boolean isJaxbBindable(Class<?> c) { try { JAXBContext.newInstance(c); return true; } catch (JAXBException e) { return false; } }","tryCatchPattern":"try { result = input.apply(XmlIO.<Record>write().withRecordClass(Record.class)...); } catch (RuntimeException e) { LOG.error(\"XmlIO record class invalid: {}\", e.getMessage(), e); throw e; }","preventionTips":["Always annotate record classes before wiring them into XmlIO.","Validate with JAXBContext.newInstance in a unit test as a build gate.","Avoid JAXB-unsupported field types (maps of unbound generics, interfaces)."],"tags":["java","apache-beam","jaxb","xml"],"backgroundTag":"schema-validation-failed","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}