title: Problems creating JAXBContext to marshal object as XML
body: <p>I have a complicated JAXB data structure generated from an XML schema. I would like to marshal it as XML solely for the purpose of logging the event. With the problems I'm having I would appreciate other solutions for this use case, but in any even here's what's wrong:</p>
  
  <p>I've found the following solution:</p>
  
  <pre><code>SomeClass object;
  java.io.StringWriter msg = new java.io.StringWriter();
  javax.xml.bind.JAXBContext context = javax.xml.bind.JAXBContext.newInstance(SomeClass.class);
  javax.xml.bind.Marshaller marshaller = context.createMarshaller();
  // Create a root element since the object is not the root element.
  javax.xml.bind.JAXBElement&lt;SomeClass&gt; tolog = new javax.xml.bind.JAXBElement&lt;SomeClass&gt;(
      new javax.xml.namespace.QName("SomeClass"), SomeClass.class, object);
  marshaller.marshal(tolog, msg);
  // Output msg somewhere, like sysout:
  System.out.println(msg.toString());
  </code></pre>
  
  <p>I've tried this two ways, each of which caused its own error.</p>
  
  <ol>
  <li>I created the JAXBContext object in static context to avoid recreating it every time. This seemed to mess with the class registry somehow because only if I called <code>JAXBContext.newInstance(SomeClass.class)</code> in static time, I got <code>java.lang.NoClassDefFoundError</code> when trying to instantiate custom classes.</li>
  <li><p>I created the JAXBContext object when first needed (which is inside an EJB's method that takes the JAXB object as an argument), storing it to a static variable after the first time. This time, the call to <code>JAXBContext.newInstance(SomeClass.class)</code> threw the following exception:</p>
  
  <pre><code>java.lang.ClassCastException: mypackage._1.NameType$JaxbAccessorF_value cannot be cast to com.sun.xml.bind.v2.runtime.reflect.Accessor
    at com.sun.xml.bind.v2.runtime.reflect.opt.OptimizedAccessorFactory.instanciate(OptimizedAccessorFactory.java:199)
    at com.sun.xml.bind.v2.runtime.reflect.opt.OptimizedAccessorFactory.get(OptimizedAccessorFactory.java:191)
    at com.sun.xml.bind.v2.runtime.reflect.Accessor$FieldReflection.optimize(Accessor.java:282)
    at com.sun.xml.bind.v2.runtime.reflect.TransducedAccessor$CompositeTransducedAccessorImpl.&lt;init&gt;(TransducedAccessor.java:237)
    at com.sun.xml.bind.v2.runtime.reflect.TransducedAccessor.get(TransducedAccessor.java:177)
    at com.sun.xml.bind.v2.model.impl.RuntimeClassInfoImpl.calcTransducer(RuntimeClassInfoImpl.java:245)
    at com.sun.xml.bind.v2.model.impl.RuntimeClassInfoImpl.getTransducer(RuntimeClassInfoImpl.java:219)
    at com.sun.xml.bind.v2.model.impl.RuntimeClassInfoImpl.link(RuntimeClassInfoImpl.java:196)
    at com.sun.xml.bind.v2.model.impl.ModelBuilder.link(ModelBuilder.java:399)
    at com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.link(RuntimeModelBuilder.java:138)
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:464)
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl.&lt;init&gt;(JAXBContextImpl.java:298)
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl.&lt;init&gt;(JAXBContextImpl.java:141)
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1163)
    at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:145)
    at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:236)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:172)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:132)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:334)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:431)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:394)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:298)
    at mypackage.MainBean.SOAPCall(MainBean.java:108)
  </code></pre></li>
  </ol>
  
  <p>I've received a request for the dependency tree. Here it is:</p>
  
  <pre><code>[INFO] mypackage:Main:war:0.0.1-SNAPSHOT
  [INFO] +- mypackage:datatypes:jar:1.0.5-SNAPSHOT:compile
  [INFO] +- org.apache.commons:commons-lang3:jar:3.3.2:compile
  [INFO] +- org.slf4j:slf4j-jdk14:jar:1.7.6:compile
  [INFO] |  \- org.slf4j:slf4j-api:jar:1.7.6:compile
  [INFO] +- org.apache.openejb:javaee-api:jar:6.0-5:provided
  [INFO] +- junit:junit:jar:4.11:test
  [INFO] |  \- org.hamcrest:hamcrest-core:jar:1.3:test
  [INFO] +- org.apache.openejb:tomee-embedded:jar:1.0.0:test
  [INFO] |  +- org.apache.openejb:openejb-jsf:jar:4.0.0:test
  [INFO] |  |  +- org.apache.openwebbeans:openwebbeans-impl:jar:1.1.4:test
  [INFO] |  |  |  \- net.sf.scannotation:scannotation:jar:1.0.2:test
  [INFO] |  |  +- org.apache.myfaces.core:myfaces-api:jar:2.1.7:test
  [INFO] |  |  \- org.apache.openejb.patch:openejb-openwebbeans-jsf:jar:1.1.4:test
  [INFO] |  +- org.apache.openejb:openejb-core:jar:4.0.0:test
  [INFO] |  |  +- org.apache.openejb:mbean-annotation-api:jar:4.0.0:test
  [INFO] |  |  +- org.apache.openejb:openejb-api:jar:4.0.0:test
  [INFO] |  |  +- org.apache.openejb:openejb-loader:jar:4.0.0:test
  [INFO] |  |  +- org.apache.openejb:openejb-javaagent:jar:4.0.0:test
  [INFO] |  |  +- org.apache.openejb:openejb-jee:jar:4.0.0:test
  [INFO] |  |  |  \- com.sun.xml.bind:jaxb-impl:jar:2.2.5:test
  [INFO] |  |  +- commons-cli:commons-cli:jar:1.2:test
  [INFO] |  |  +- org.apache.activemq:kahadb:jar:5.5.1:test
  [INFO] |  |  +- org.apache.activemq:activemq-ra:jar:5.5.1:test
  [INFO] |  |  +- org.apache.activemq:activemq-core:jar:5.5.1:test
  [INFO] |  |  |  +- org.apache.geronimo.specs:geronimo-jms_1.1_spec:jar:1.1.1:test
  [INFO] |  |  |  +- org.apache.activemq:activeio-core:jar:3.1.2:test
  [INFO] |  |  |  +- org.apache.activemq.protobuf:activemq-protobuf:jar:1.1:test
  [INFO] |  |  |  +- org.apache.geronimo.specs:geronimo-j2ee-management_1.1_spec:jar:1.0.1:test
  [INFO] |  |  |  \- commons-net:commons-net:jar:2.0:test
  [INFO] |  |  +- org.apache.geronimo.components:geronimo-connector:jar:3.1.1:test
  [INFO] |  |  |  \- org.apache.geronimo.specs:geronimo-j2ee-connector_1.6_spec:jar:1.0:test
  [INFO] |  |  +- org.apache.geronimo.components:geronimo-transaction:jar:3.1.1:test
  [INFO] |  |  +- org.objectweb.howl:howl:jar:1.0.1-1:test
  [INFO] |  |  +- org.apache.geronimo.javamail:geronimo-javamail_1.4_mail:jar:1.8.2:test
  [INFO] |  |  +- org.apache.xbean:xbean-asm-shaded:jar:3.10:test
  [INFO] |  |  +- org.apache.openejb.patch:xbean-finder-shaded:jar:3.10:test
  [INFO] |  |  +- org.apache.xbean:xbean-reflect:jar:3.10:test
  [INFO] |  |  +- org.apache.xbean:xbean-naming:jar:3.10:test
  [INFO] |  |  +- org.apache.xbean:xbean-bundleutils:jar:3.10:test
  [INFO] |  |  +- org.hsqldb:hsqldb:jar:2.2.4:test
  [INFO] |  |  +- commons-dbcp:commons-dbcp:jar:1.4:test
  [INFO] |  |  +- commons-pool:commons-pool:jar:1.5.7:test
  [INFO] |  |  +- org.codehaus.swizzle:swizzle-stream:jar:1.6.1:test
  [INFO] |  |  +- wsdl4j:wsdl4j:jar:1.6.2:test
  [INFO] |  |  +- org.quartz-scheduler:quartz:jar:2.1.3:test
  [INFO] |  |  +- org.apache.openwebbeans:openwebbeans-spi:jar:1.1.4:test
  [INFO] |  |  +- org.apache.openwebbeans:openwebbeans-ejb:jar:1.1.4:test
  [INFO] |  |  +- org.apache.openwebbeans:openwebbeans-ee:jar:1.1.4:test
  [INFO] |  |  +- org.apache.openwebbeans:openwebbeans-ee-common:jar:1.1.4:test
  [INFO] |  |  +- org.apache.openwebbeans:openwebbeans-web:jar:1.1.4:test
  [INFO] |  |  +- org.javassist:javassist:jar:3.15.0-GA:test
  [INFO] |  |  +- org.apache.openejb.patch:openjpa-asm-shaded:jar:2.2.0:test
  [INFO] |  |  |  \- net.sourceforge.serp:serp:jar:1.13.1:test
  [INFO] |  |  +- org.apache.bval:bval-core:jar:0.4:test
  [INFO] |  |  |  \- commons-beanutils:commons-beanutils-core:jar:1.8.3:test
  [INFO] |  |  \- org.apache.bval:bval-jsr303:jar:0.4:test
  [INFO] |  +- org.apache.myfaces.core:myfaces-impl:jar:2.1.7:test
  [INFO] |  |  +- commons-collections:commons-collections:jar:3.2:test
  [INFO] |  |  +- commons-beanutils:commons-beanutils:jar:1.8.3:test
  [INFO] |  |  |  \- commons-logging:commons-logging:jar:1.1.1:test
  [INFO] |  |  \- commons-digester:commons-digester:jar:1.8:test
  [INFO] |  +- javax.servlet:jstl:jar:1.2:test
  [INFO] |  +- org.apache.tomcat:tomcat-catalina:jar:7.0.27:test
  [INFO] |  |  +- org.apache.tomcat:tomcat-servlet-api:jar:7.0.27:test
  [INFO] |  |  +- org.apache.tomcat:tomcat-juli:jar:7.0.27:test
  [INFO] |  |  +- org.apache.tomcat:tomcat-annotations-api:jar:7.0.27:test
  [INFO] |  |  +- org.apache.tomcat:tomcat-api:jar:7.0.27:test
  [INFO] |  |  \- org.apache.tomcat:tomcat-util:jar:7.0.27:test
  [INFO] |  +- org.apache.tomcat:tomcat-catalina-ha:jar:7.0.27:test
  [INFO] |  |  \- org.apache.tomcat:tomcat-tribes:jar:7.0.27:test
  [INFO] |  +- org.apache.tomcat:tomcat-coyote:jar:7.0.27:test
  [INFO] |  +- org.apache.tomcat:tomcat-jasper:jar:7.0.27:test
  [INFO] |  |  +- org.apache.tomcat:tomcat-jsp-api:jar:7.0.27:test
  [INFO] |  |  +- org.apache.tomcat:tomcat-el-api:jar:7.0.27:test
  [INFO] |  |  \- org.apache.tomcat:tomcat-jasper-el:jar:7.0.27:test
  [INFO] |  +- org.eclipse.jdt.core.compiler:ecj:jar:3.5.1:test
  [INFO] |  +- org.apache.velocity:velocity:jar:1.6.4:test
  [INFO] |  |  +- commons-lang:commons-lang:jar:2.4:test
  [INFO] |  |  \- oro:oro:jar:2.0.8:test
  [INFO] |  +- org.apache.geronimo.specs:geronimo-j2ee-deployment_1.1_spec:jar:1.1:test
  [INFO] |  +- org.apache.openejb:openejb-client:jar:4.0.0:test
  [INFO] |  \- org.apache.openejb:tomee-catalina:jar:4.0.0:test
  [INFO] |     +- org.apache.openejb:javaee-api:jar:tomcat:6.0-3:test
  [INFO] |     +- org.apache.openejb:tomee-common:jar:4.0.0:test
  [INFO] |     +- org.apache.openejb:tomee-loader:jar:4.0.0:test
  [INFO] |     |  \- com.google.code.gson:gson:jar:2.1:test
  [INFO] |     +- org.apache.openejb:openejb-server:jar:4.0.0:test
  [INFO] |     +- org.apache.openejb:openejb-ejbd:jar:4.0.0:test
  [INFO] |     \- org.apache.openejb:openejb-http:jar:4.0.0:test
  [INFO] \- commons-codec:commons-codec:jar:1.3:compile
  </code></pre>
  
  <p>What is going wrong, and how do I make it go right instead?</p>