title: Supplying a different version of JAXB for JAX-WS in Java 1.6
body: <p>I have a third party jar that ships with a jaxb-impl.jar and includes it in its manifest classpath.  The problem is, it seems as though supplying your own version of JAXB (regardless of which version it may be) seems to break the SoapFaultBuilder in JAX-WS.</p>
  
  <p>According to the <a href="http://jaxb.java.net/guide/Migrating_JAXB_2_0_applications_to_JavaSE_6.html" rel="noreferrer">Unofficial JAXB Guide</a>, it seems as though Sun deliberately changed the package name when it folded JAXB into the JDK in order to avoid conflicts with the stand-alone version.  However, the SoapFaultBuilder (part of JAX-WS I believe) that ships with the JDK is explicitly dependent on the new, internal package name.  This causes it to fail when building a fault message if you've added a stand-alone JAXB jar (even if it is the same version <strong>number</strong> of JAXB).</p>
  
  <p>Here is my small test case: I make a trivial Web Service:</p>
  
  <pre><code>package wstest;
  
  import javax.jws.WebMethod;
  import javax.jws.WebService;
  import javax.jws.soap.SOAPBinding;
  import javax.jws.soap.SOAPBinding.Style;
  
  //Service Endpoint Interface
  @WebService
  @SOAPBinding(style = Style.RPC)
  public interface HelloWorld{
  
      @WebMethod String getHelloWorldAsString(String name);
  
  }
  </code></pre>
  
  <p>And an implementation that simply throws an exception.  (Since the problem only occurs in the SOAPFaultBuilder):</p>
  
  <pre><code>package wstest;
  
  import javax.jws.WebService;
  
  //Service Implementation
  @WebService(endpointInterface = "wstest.HelloWorld")
  public class HelloWorldImpl implements HelloWorld{
  
      @Override
      public String getHelloWorldAsString(String name) {
          //return "Hello World JAX-WS " + name;
          throw new RuntimeException("Exception for: " + name);
      }
  
  }
  </code></pre>
  
  <p>And a class to publish the web service:</p>
  
  <pre><code>package wstest;
  
  import javax.xml.ws.Endpoint;
  
  //Endpoint publisher
  public class HelloWorldPublisher{
  
      public static void main(String[] args) {
         Endpoint.publish("http://localhost:9999/ws/hello", new HelloWorldImpl());
      }
  
  }
  </code></pre>
  
  <p>I run the HelloWorldPublisher, and then run this client against it:</p>
  
  <pre><code>package wstest;
  
  import java.net.URL;
  import javax.xml.namespace.QName;
  import javax.xml.ws.Service;
  
  public class HelloWorldClient{
  
      public static void main(String[] args) throws Exception {
  
      URL url = new URL("http://localhost:9999/ws/hello?wsdl");
  
          //1st argument service URI, refer to wsdl document above
      //2nd argument is service name, refer to wsdl document above
          QName qname = new QName("http://wstest/", "HelloWorldImplService");
  
          Service service = Service.create(url, qname);
  
          HelloWorld hello = service.getPort(HelloWorld.class);
  
          System.out.println(hello.getHelloWorldAsString("Matt"));
  
      }
  
  }
  </code></pre>
  
  <p>This correctly spits out the exception that was thrown by the Web Service.  However, when I add any version of jaxb-impl.jar, whether on the classpath or in the endorsed lib, I get this stack trace:</p>
  
  <pre><code>Exception in thread "main" java.lang.ExceptionInInitializerError
      at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:107)
      at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:78)
      at com.sun.xml.internal.ws.client.sei.SEIStub.invoke(SEIStub.java:107)
      at $Proxy19.getHelloWorldAsString(Unknown Source)
      at wstest.HelloWorldClient.main(HelloWorldClient.java:21)
  Caused by: java.lang.ClassCastException: com.sun.xml.bind.v2.runtime.JAXBContextImpl cannot be cast to com.sun.xml.internal.bind.api.JAXBRIContext
      at com.sun.xml.internal.ws.fault.SOAPFaultBuilder.&lt;clinit&gt;(SOAPFaultBuilder.java:533)
      ... 5 more
  </code></pre>
  
  <p>The exception occurs because com.sun.xml.bind.v2.runtime.JAXBContextImpl from my jaxb-impl extends com.sun.xml.bind.api.JAXBRIContext instead of com.sun.xml.internal.bind.api.JAXBRIContext (note the missing 'internal' sub-package in the package hierarchy).</p>
  
  <p>Also according to the <a href="http://jaxb.java.net/guide/Migrating_JAXB_2_0_applications_to_JavaSE_6.html" rel="noreferrer">Unofficial JAXB Guide</a>, they say you need to use endorsed lib in order to correctly override the version of JAXB.  As it turns out though, SOAPFaultBuilder uses JAXBContext.newInstance() to search the classpath for a file named <code>/META-INF/services/javax.xml.bind.JAXBContext</code>, then manually load (and reflexively create) a JAXBContext based on the class name specified in the file.  So it doesn't matter - classpath or endorsed lib gives you the same behavior.</p>
  
  <p>One workaround is to add <code>-Djavax.xml.bind.JAXBContext=com.sun.xml.internal.bind.v2.ContextFactory</code> to the command line, which causes JAXBContext.newInstance() to ignore the <code>/META-INF/services/javax.xml.bind.JAXBContext</code> file on the classpath and manually specifies the built-in version of JAXB.  The other workaround is to simply not specify your own JAXB and use the version built into the JDK, but it seems from the Unofficial JAXB Guide, that Sun designed this system to be able to handle supplying your own JAXB implementation.  Has anyone been able to successfully supply a version of JAXB and still be able to successfully capture fault messages?  (Everything runs fine for me as long as there are no faults generated by the Web Service).</p>