title: JAXBContext.newInstance variations
body: <p>I am experimenting with the various forms of <strong>newInstance</strong> method in class <strong>JAXBContext</strong> (I am using the default Sun JAXB implementation that ships with Oracle JDK 1.7).</p>
  
  <p>It's not clear to me when it's ok to just pass to the <strong>newInstance</strong> method the concrete classes versus the <strong>ObjectFactory</strong> class. I should note that I am using JAXB purely for parsing XML files, i.e. only in the XML->Java direction.</p>
  
  <p>Here's the absolutely minimal code that demonstrates my point:</p>
  
  <h2>xsd file</h2>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;schema elementFormDefault="qualified"
      xmlns          ="http://www.w3.org/2001/XMLSchema"
      xmlns:a        ="http://www.example.org/A"
      targetNamespace="http://www.example.org/A"&gt;
      &lt;element name="root" type="a:RootType"&gt;&lt;/element&gt;
  
      &lt;complexType name="RootType"&gt;
         &lt;sequence&gt;
             &lt;element name="value" type="string"&gt;&lt;/element&gt;
         &lt;/sequence&gt;
      &lt;/complexType&gt;
  &lt;/schema&gt;
  </code></pre>
  
  <p>Given the above XSD, the following <strong>JAXBInstance.newInstance</strong> invocations succeed in creating a context that can parse a sample <strong>a.xml</strong> file:</p>
  
  <ul>
  <li>jc = JAXBContext.newInstance("example.a");</li>
  <li>jc = JAXBContext.newInstance(example.a.ObjectFactory.class); </li>
  <li>jc = JAXBContext.newInstance(example.a.RootType.class, example.a.ObjectFactory.class);</li>
  </ul>
  
  <p>However, passing the <strong>example.a.RootType.class</strong> alone fails with <strong>javax.xml.bind.UnmarshalException</strong> at runtime:</p>
  
  <pre><code>jc = JAXBContext.newInstance(example.a.RootType.class); // this fails at runtime.
  </code></pre>
  
  <p>Can anyone shed some light? The reason I am experimenting on these <strong>JAXBContext::newInstance</strong> variations is that I've stumbled on <a href="https://stackoverflow.com/questions/4274482/jaxb-xsdinclude-and-separate-packages">this problem</a> where the accepted answer included the option of "building the JAXB context based on individual classes rather than object factories". The sample <strong>a.xml</strong> and the <strong>JAXB</strong> Java code I am using follow at the end of the post.</p>
  
  <h1>sample a.xml used</h1>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;a:root xmlns:a="http://www.example.org/A"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://www.example.org/A A.xsd"&gt;
      &lt;a:value&gt;foo&lt;/a:value&gt;
  &lt;/a:root&gt;
  </code></pre>
  
  <h1>JAXB parsing code</h1>
  
  <pre><code>public static void main (String args[]) throws JAXBException, FileNotFoundException {
      JAXBContext jc = null;
      message("using package context (press any key:)");
      jc = JAXBContext.newInstance("example.a");
      work(jc); // SUCCEEDS
  
      message("using Object factory (press any key):");
      jc = JAXBContext.newInstance(example.a.ObjectFactory.class);
      work(jc); // SUCCEEDS
  
      message("using class enumeration (press any key):");
      try {
          jc = JAXBContext.newInstance(example.a.RootType.class);
          work(jc);  // FAILS
      } catch (javax.xml.bind.UnmarshalException e) {
          e.printStackTrace();
      }
  
      message("using class enumeration and Object factory too (press any key):");
      jc = JAXBContext.newInstance(example.a.RootType.class, example.a.ObjectFactory.class);
      work(jc); // SUCCEEDS
  
  }
  
  private static void work(JAXBContext jc) throws JAXBException, FileNotFoundException {
      Unmarshaller u = jc.createUnmarshaller();
      RootType root = ((JAXBElement&lt;RootType&gt;)u.unmarshal( new FileInputStream( "a.xml" ))).getValue();
      System.out.println( root.getValue() );
  }
  </code></pre>