I have a relatively simple package of 8 Java classes generated from an XML schema using JAXB XJC. I also have a utility class to marshal and unmarshal instances of the class.
This works
The utility class can successfully unmarshal a valid XML document into an instance of the ‘root’ class WordMergeInfo. For example this works fine:
JAXBContext jc = JAXBContext.newInstance(WordMergeInfo.class);
Unmarshaller um = jc.createUnmarshaller();
return (WordMergeInfo)um.unmarshal(inputStream);
This doesn’t work
But marshalling to a string fails. In this code:
JAXBContext jc = JAXBContext.newInstance(WordMergeInfo.class);
Marshaller m = jc.createMarshaller();
StringWriter writer = new StringWriter();
m.marshal(m, writer);
return writer.toString();
the call to Marshaller.marshal fails with the following error:
javax.xml.bind.JAXBException: class com.sun.xml.bind.v2.runtime.MarshallerImpl nor any of its super class is known to this context.
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:594)
at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:482)
at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:315)
at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:244)
As I understand it, nor any of its super class is known to this context means that a JAXB class needed for marshalling cannot be found. So why can’t one of the JAXB implementation classes be found, when the same class is in the stack trace?
Context
This error showed up in a unit test of my class, run under Maven. The dependencies are:
- javax.xml.bind:jaxb-api:2.1
- com.sun.xml.bind:jaxb-impl:2.1.13
I got the same error with earlier versions of these (2.0 and 2.0.3, respectively).
The Maven test class path is:
C:\Users\mstra.CUSTMAN\Workspace\DARTCorrModule\xml\target\test-classes
C:\Users\mstra.CUSTMAN\Workspace\DARTCorrModule\xml\target\classes
C:\Users\mstra.CUSTMAN\.m2\repository\javax\xml\bind\jaxb-api\2.1\jaxb-api-2.1.jar
C:\Users\mstra.CUSTMAN\.m2\repository\javax\xml\stream\stax-api\1.0-2\stax-api-1.0-2.jar
C:\Users\mstra.CUSTMAN\.m2\repository\javax\activation\activation\1.1\activation-1.1.jar
C:\Users\mstra.CUSTMAN\.m2\repository\com\sun\xml\bind\jaxb-impl\2.1.13\jaxb-impl-2.1.13.jar
C:\Users\mstra.CUSTMAN\.m2\repository\junit\junit\4.8.2\junit-4.8.2.jar
C:\Users\mstra.CUSTMAN\.m2\repository\org\mockito\mockito-all\1.8.5\mockito-all-1.8.5.jar
C:\Users\mstra.CUSTMAN\.m2\repository\javax\ejb\ejb-api\3.0\ejb-api-3.0.jar
C:\Users\mstra.CUSTMAN\.m2\repository\org\slf4j\slf4j-api\1.6.4\slf4j-api-1.6.4.jar
Any insight is appreciated.
This means that the class is not registered as a marshallable class in the JAXB context.
Your error is on this line:
You are trying to marshal the marshaller itself. You probably meant to marshal a
WordMergeInfoobject.