{"record":{"id":"25341f68288eec22","repo":"apache/beam","slug":"error-when-creating-marshaller-from-jaxb-context","errorCode":null,"errorMessage":"Error when creating marshaller from JAXB Context.","messagePattern":"Error when creating marshaller from JAXB Context\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"sdks/java/io/xml/src/main/java/org/apache/beam/sdk/io/xml/JAXBCoder.java","lineNumber":73,"sourceCode":"  private transient volatile XMLInputFactory xmlInputFactory;\n  private final EmptyOnDeserializationThreadLocal<Marshaller> jaxbMarshaller;\n  private final EmptyOnDeserializationThreadLocal<Unmarshaller> jaxbUnmarshaller;\n\n  public Class<T> getJAXBClass() {\n    return jaxbClass;\n  }\n\n  private JAXBCoder(Class<T> jaxbClass) {\n    this.jaxbClass = jaxbClass;\n    this.jaxbMarshaller =\n        new EmptyOnDeserializationThreadLocal<Marshaller>() {\n          @Override\n          protected Marshaller initialValue() {\n            try {\n              JAXBContext jaxbContext = getContext();\n              return jaxbContext.createMarshaller();\n            } catch (JAXBException e) {\n              throw new RuntimeException(\"Error when creating marshaller from JAXB Context.\", e);\n            }\n          }\n        };\n    this.jaxbUnmarshaller =\n        new EmptyOnDeserializationThreadLocal<Unmarshaller>() {\n          @Override\n          protected Unmarshaller initialValue() {\n            try {\n              JAXBContext jaxbContext = getContext();\n              return jaxbContext.createUnmarshaller();\n            } catch (Exception e) {\n              throw new RuntimeException(\"Error when creating unmarshaller from JAXB Context.\", e);\n            }\n          }\n        };\n  }\n\n  /**","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/io/xml/src/main/java/org/apache/beam/sdk/io/xml/JAXBCoder.java#L55-L91","documentation":"JAXBCoder lazily creates a thread-local JAXB Marshaller for XML serialization. If JAXBContext.createMarshaller() throws a JAXBException (bad context configuration, invalid class setup), the ThreadLocal initialValue wraps it in this RuntimeException. It indicates the coder cannot produce a marshaller and XML writes will fail.","triggerScenarios":"Constructing a JAXBCoder whose JAXBContext was built from classes that are not properly JAXB-annotated or whose context fails, then attempting to encode an element, which triggers the ThreadLocal's initialValue.","commonSituations":"Annotating POJOs incorrectly for JAXB, missing @XmlRootElement/@XmlAccessorType annotations, mixing JAXB implementations after Java 11 removal of javax.xml.bind from the JDK, or conflicting JAXB versions on the classpath.","solutions":["Inspect the chained JAXBException cause for the real problem and fix the annotated classes accordingly.","Ensure all classes passed to JAXBCoder/XMLIO are valid JAXB-annotated classes.","On Java 11+, add an explicit JAXB implementation dependency (e.g. org.glassfish.jaxb:jaxb-runtime) and align versions.","Test JAXBContext.newInstance(recordClass).createMarshaller() in isolation to reproduce and diagnose."],"exampleFix":"// before: class without JAXB annotations passed to coder\nCoder<MyPojo> coder = Jaxbcoder.of(MyPojo.class);\n// after\n@XmlRootElement\n@XmlAccessorType(XmlAccessType.FIELD)\npublic class MyPojo { ... }","handlingStrategy":"try-catch","validationCode":"try ( Marshaller unused = JAXBContext.newInstance(recordClass).createMarshaller() ) { /* ok */ } catch (JAXBException e) { throw new IllegalStateException(\"Record class not JAXB-marshalable\", e); }","typeGuard":null,"tryCatchPattern":"try { coder = Jaxbcoder.of(Record.class); } catch (RuntimeException e) { throw new IllegalStateException(\"JAXB marshaller setup failed: \" + e.getCause(), e); }","preventionTips":["Annotate all record classes with @XmlRootElement and @XmlAccessorType(FIELD).","Include jaxb-runtime in the pipeline fat jar when using Java 11+.","Smoke-test JAXBContext creation in a unit test before deploying pipelines."],"tags":["java","apache-beam","jaxb","xml"],"backgroundTag":"json-marshal-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"}