title: Issues with EclipseLink MOXy OSGi bundle
body: <p>I am working on a a very simple Eclipse Plug-in with which uses EclipseLink's MOXy for XML binding (marshalling/unmarshalling). I get a couple of errors when I try to marshall a simple class (Person.java) during the starting process of the plugin (Activator.java). Here's the code:</p>
  
  <h2>CODE:</h2>
  
  <p><strong>Person.java</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>package test_rcp;
  
  import javax.xml.bind.annotation.XmlRootElement;
  
  @XmlRootElement
  public class Person {
  
      private String name;
      private int age;
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public int getAge() {
          return age;
      }
  
      public void setAge(int age) {
          this.age = age;
      }
  
  }
  </code></pre>
  
  <p><strong>Activator.java</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>package test_rcp;
  
  import javax.xml.bind.JAXBContext;
  import javax.xml.bind.Marshaller;
  
  import org.eclipse.ui.plugin.AbstractUIPlugin;
  import org.osgi.framework.BundleContext;
  
  public class Activator extends AbstractUIPlugin {
  
      public void start(BundleContext context) throws Exception {
          super.start(context);
          plugin = this;
  
          // Just to make sure EclipseLink is already loaded
          System.out.println(org.eclipse.persistence.Version.getVersion());
  
          Person p1 = new Person();
          p1.setName("John Dewey");
          p1.setAge(54);
          JAXBContext jc = JAXBContext.newInstance("test_rcp",Person.class.getClassLoader());
  
           Marshaller marshaller = jc.createMarshaller();
           marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
           marshaller.marshal(p1, System.out);
      }
  }
  </code></pre>
  
  <p><strong>MANIFEST.MF</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>Manifest-Version: 1.0
  Bundle-ManifestVersion: 2
  Bundle-Name: Test-RCP
  Bundle-SymbolicName: Test-RCP; singleton:=true
  Bundle-Version: 1.0.0.qualifier
  Bundle-Activator: test_rcp.Activator
  Require-Bundle: org.eclipse.ui,
   org.eclipse.core.runtime,
   org.eclipse.persistence.core,
   org.eclipse.persistence.moxy;bundle-version="2.4.1"
  Bundle-ActivationPolicy: lazy
  Bundle-RequiredExecutionEnvironment: JavaSE-1.7
  </code></pre>
  
  <p>The package <code>test_rcp</code> also contains <code>jaxb.index</code> and <code>jaxb.properties</code>:</p>
  
  <p><strong>jaxb.index</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>Person
  </code></pre>
  
  <p><strong>jaxb.properties</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
  </code></pre>
  
  <h2>PROBLEM:</h2>
  
  <p>Running the plug-in however throws the following exception:</p>
  
  <pre><code>javax.xml.bind.JAXBException: Provider org.eclipse.persistence.jaxb.JAXBContextFactory could not be instantiated: 
  javax.xml.bind.JAXBException: Provider class org.eclipse.persistence.jaxb.JAXBContextFactory could not be instantiated:
   javax.xml.bind.JAXBException:
   ClassCastException: attempting to cast bundleresource://43.fwk583158213/javax/xml/bind/JAXBContext.class to jar:file:/usr/lib/jvm/java-7-oracle/jre/lib/rt.jar!/javax/xml/bind/JAXBContext.class.  Please make sure that you are specifying the proper ClassLoader.
  </code></pre>
  
  <p>I understand that the problem arises from the fact that two class loaders have loaded <code>javax.xml.bind</code> package, but I cannot figure out why and how to avoid this.</p>
  
  <p>Morover, by using <code>JAXBContextFactory</code> instead of <code>JAXBContext.newInstance</code>(as suggested <a href="https://stackoverflow.com/a/11797373/2295964">here</a>), i.e.:</p>
  
  <pre class="lang-java prettyprint-override"><code>JAXBContext jc = org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(new Class[] {Person.class}, null);
  </code></pre>
  
  <p>I would get the following exception:</p>
  
  <pre><code>java.lang.LinkageError: loader constraint violation:
  loader (instance of org/eclipse/osgi/internal/baseadaptor/DefaultClassLoader) previously
  initiated loading for a different type with name "javax/xml/bind/JAXBContext"
  </code></pre>
  
  <p>I have already tried out following solutions with no success:</p>
  
  <p><a href="https://stackoverflow.com/a/12943115/2295964">https://stackoverflow.com/a/12943115/2295964</a></p>
  
  <p><a href="https://stackoverflow.com/a/11797373/2295964">https://stackoverflow.com/a/11797373/2295964</a></p>
  
  <p><strong><em>I would really appreciate if someone could help me getting out of this misery!</em></strong></p>