title: Parsing xml file with JAXB
body: <p>I'm implementing a simple xml parser and faced with a problem:
  exception is thrown when initialising JAXBContext.</p>
  <p>Here is my code(maybe too much for this question, but I've tried to include only important parts):</p>
  <p>Parser itself:</p>
  <pre class="lang-java prettyprint-override"><code>package com.andreiyusupau.jaxbparser.parser;
  
  import com.andreiyusupau.jaxbparser.model.Parent;
  import com.andreiyusupau.jaxbparser.model.Root;
  
  import javax.xml.bind.JAXBContext;
  import javax.xml.bind.JAXBException;
  import javax.xml.bind.Unmarshaller;
  import java.io.File;
  import java.util.Collection;
  
  public class JaxbParser {
  
      public Collection&lt;Parent&gt; parse(String inputXml) throws JAXBException  {
              JAXBContext jc = JAXBContext.newInstance(Root.class);
              Unmarshaller unmarshaller = jc.createUnmarshaller();
              Root root = (Root) unmarshaller.unmarshal(new File(inputXml));
              return root.getElements();
      }
  }
  </code></pre>
  <p>Main:</p>
  <pre class="lang-java prettyprint-override"><code>public class Main {
  
      public static final String FILE_NAME = &quot;data.xml&quot;;
  
      public static void main(String[] args) throws JAXBException {
          JaxbParser jaxbParser = new JaxbParser();
         Collection&lt;Parent&gt; elements= jaxbParser.parse(FILE_NAME);
          for (Parent element : elements) {
              System.out.println(element.toString());
          }
      }
  }
  </code></pre>
  <p>Classes (getters,setters and toString are hidden):</p>
  <pre class="lang-java prettyprint-override"><code>
  import javax.xml.bind.annotation.*;
  import java.util.List;
  
  @XmlRootElement(name = &quot;root&quot;)
  @XmlAccessorType(XmlAccessType.FIELD)
  public class Root {
      @XmlElements({
              @XmlElement(name = &quot;ChildA&quot;, type = ChildA.class),
              @XmlElement(name = &quot;ChildB&quot;, type = ChildB.class),
      })
      private List&lt;Parent&gt; elements;
  
      public Root() {
      }
  }
  
  </code></pre>
  <pre class="lang-java prettyprint-override"><code>import org.eclipse.persistence.oxm.annotations.XmlPath;
  
  import javax.xml.bind.annotation.XmlAttribute;
  import javax.xml.bind.annotation.XmlElement;
  import javax.xml.bind.annotation.XmlSeeAlso;
  import javax.xml.bind.annotation.XmlTransient;
  
  @XmlTransient
  @XmlSeeAlso({ChildA.class, ChildB.class})
  public class Parent {
      @XmlAttribute
      private int id;
      @XmlElement(name = &quot;name&quot;)
      private String name;
      @XmlElement(name = &quot;parameter&quot;)
      private String parameter;
      @XmlPath(&quot;parameter/@parameterAttr&quot;)
      private String parameterAttribute;
      @XmlPath(&quot;similar/a/text()&quot;)
      private int a;
      @XmlPath(&quot;similar/b/text()&quot;)
      private int b;
      @XmlPath(&quot;different/similar/text()&quot;)
      private String similar;
      @XmlPath(&quot;different/similar/@similarAttr&quot;)
      private String similarAttribute;
  
      public Parent() {
      }
  }
  </code></pre>
  <pre class="lang-java prettyprint-override"><code>import org.eclipse.persistence.oxm.annotations.XmlPath;
  
  import javax.xml.bind.annotation.XmlRootElement;
  
  @XmlRootElement(name = &quot;childA&quot;)
  public class ChildA extends Parent {
      @XmlPath(&quot;different/differentA/text()&quot;)
      private String differentA;
      @XmlPath(&quot;different/differentA/@attrA&quot;)
      private String differentAttributeA;
  
      public ChildA() {
      }
  }
  </code></pre>
  <pre class="lang-java prettyprint-override"><code>import org.eclipse.persistence.oxm.annotations.XmlPath;
  
  import javax.xml.bind.annotation.XmlRootElement;
  
  @XmlRootElement(name = &quot;childB&quot;)
  public class ChildB extends Parent {
      @XmlPath(&quot;different/differentB1/text()&quot;)
      private String differentB1;
      @XmlPath(&quot;different/differentB1/@attrB1&quot;)
      private String differentAttributeB1;
      @XmlPath(&quot;different/differentB2/text()&quot;)
      private String differentB2;
      @XmlPath(&quot;different/differentB2/@attrB2&quot;)
      private String differentAttributeB2;
  
      public ChildB() {
      }
  }
  </code></pre>
  <p>and finally an xml file itself:</p>
  <pre><code>&lt;?xml version=&quot;1.1&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;root xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
           xsi:noNamespaceSchemaLocation = &quot;schema.xsd&quot;&gt;
      &lt;childA id=&quot;ID-1&quot;&gt;
          &lt;name&gt;Name&lt;/name&gt;
          &lt;parameter parameterAttr=&quot;a&quot;&gt;b&lt;/parameter&gt;
          &lt;similarGroup&gt;
              &lt;a&gt;1&lt;/a&gt;
              &lt;b&gt;2&lt;/b&gt;
          &lt;/similarGroup&gt;
          &lt;different&gt;
              &lt;similar similarAttr=&quot;n&quot;&gt;2&lt;/similar&gt;
              &lt;differentA attrA=&quot;n&quot;&gt;100&lt;/differentA&gt;
          &lt;/different&gt;
      &lt;/childA&gt;
      &lt;childB id=&quot;ID-2&quot;&gt;
          &lt;name&gt;Name&lt;/name&gt;
          &lt;parameter parameterAttr=&quot;c&quot;&gt;d&lt;/parameter&gt;
          &lt;similarGroup&gt;
              &lt;a&gt;6&lt;/a&gt;
              &lt;b&gt;8&lt;/b&gt;
          &lt;/similarGroup&gt;
          &lt;different&gt;
              &lt;similar similarAttr=&quot;g&quot;&gt;7&lt;/similar&gt;
              &lt;differentB1 attrB1=&quot;5&quot;&gt;100&lt;/differentB1&gt;
              &lt;differentB2&gt;100&lt;/differentB2&gt;
          &lt;/different&gt;
      &lt;/childB&gt;
  &lt;/root&gt;
  </code></pre>
  <p>and dependencies in POM xml:</p>
  <pre><code>    &lt;dependency&gt;
          &lt;groupId&gt;jakarta.xml.bind&lt;/groupId&gt;
          &lt;artifactId&gt;jakarta.xml.bind-api&lt;/artifactId&gt;
          &lt;version&gt;2.3.3&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.eclipse.persistence&lt;/groupId&gt;
          &lt;artifactId&gt;eclipselink&lt;/artifactId&gt;
          &lt;version&gt;3.0.0-M1&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.glassfish.jaxb&lt;/groupId&gt;
          &lt;artifactId&gt;jaxb-runtime&lt;/artifactId&gt;
          &lt;version&gt;2.3.3&lt;/version&gt;
      &lt;/dependency&gt;
  </code></pre>
  <p>jaxb.properties</p>
  <pre><code>javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
  </code></pre>
  <p>Compiling and running this code gives the following exception:</p>
  <pre><code>Exception in thread &quot;main&quot; java.lang.NoClassDefFoundError: jakarta/xml/bind/JAXBContext
      at java.base/java.lang.Class.getDeclaredMethods0(Native Method)
      at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3166)
      at java.base/java.lang.Class.getMethodsRecursive(Class.java:3307)
      at java.base/java.lang.Class.getMethod0(Class.java:3293)
      at java.base/java.lang.Class.getMethod(Class.java:2106)
      at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:249)
      at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:240)
      at javax.xml.bind.ContextFinder.find(ContextFinder.java:345)
      at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:691)
      at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:632)
      at com.andreiyusupau.jaxbparser.parser.JaxbParser.parse(JaxbParser.java:15)
      at com.andreiyusupau.jaxbparser.Main.main(Main.java:15)
  Caused by: java.lang.ClassNotFoundException: jakarta.xml.bind.JAXBContext
      at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
      at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
      at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
      ... 12 more
  </code></pre>
  <p>Maybe someone knows how can I solve it?</p>
  <p><strong>UPDATE:</strong></p>
  <p>After replacing <code>jakarta.xml.bind-api</code> with <code>jaxb-api</code> I have the following exception:</p>
  <pre><code>Exception in thread &quot;main&quot; javax.xml.bind.JAXBException: ClassCastException: attempting to cast jar:file:/C:/Users/Nevermind/.m2/repository/javax/xml/bind/jaxb-api/2.3.1/jaxb-api-2.3.1.jar!/javax/xml/bind/JAXBContext.class to jar:file:/C:/Users/Nevermind/.m2/repository/javax/xml/bind/jaxb-api/2.3.1/jaxb-api-2.3.1.jar!/javax/xml/bind/JAXBContext.class.  Please make sure that you are specifying the proper ClassLoader.    
      at javax.xml.bind.ContextFinder.handleClassCastException(ContextFinder.java:157)
      at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:300)
      at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:286)
      at javax.xml.bind.ContextFinder.find(ContextFinder.java:391)
      at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:721)
      at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:662)
      at com.andreiyusupau.jaxbparser.parser.JaxbParser.parse(JaxbParser.java:15)
      at com.andreiyusupau.jaxbparser.Main.main(Main.java:15)
  </code></pre>
  <p>The exception is thrown in line:
  <code>JAXBContext jc = JAXBContext.newInstance(Root.class);</code></p>
  <p>Any suggestions?</p>