1

I'm trying to use the MOxy implementation of the JAXB specification. One of the primary reasons is to make sure unmapped elements in the XML file are not lost during marshaling back the content of the objects. I've been hitting the ClassCastException even though (I think) I have the jaxb.properties file in the right location. Following is my file organization (as well as how it is organized in Eclipse) :

  • \zMain.java
  • \Config.java
  • \jaxb.properties (specifying to use MOxy has the JAXB provider - javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory)

I have some basic stuff written to validate functioning of this implementation.

import java.io.File;
import javax.xml.bind.Binder;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.eclipse.persistence.jaxb.JAXBContext;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

public class zMain {
   public static void main(String[] args) {
      try {
         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
         DocumentBuilder db = dbf.newDocumentBuilder();
         File xml = new File("sampleconfig.xml");
         Document document = db.parse(xml);
         JAXBContext jc = JAXBContext.newInstance(Config.class);
         Binder<Node> binder = jc.createBinder();
         Config cfg = (Config) binder.unmarshal(document);
         // do nothing
         binder.updateXML(cfg);
         TransformerFactory tf = TransformerFactory.newInstance();
         Transformer t = tf.newTransformer();
         t.setOutputProperty(OutputKeys.INDENT, "yes");
         t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3");
         t.transform(new DOMSource(document), new StreamResult(System.out));
      } catch (Exception ex) {
         ex.printStackTrace();
         System.exit(100);
      }
   }
}

Executing this JAR throws

java.lang.ClassCastException: com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl cannot be cast to org.eclipse.persistence.jaxb.JAXBContext

eclipselink-2.4.0.jar is in my classpath (else I'm assuming it would've been a ClassNotFoundException or a compilation error).

Though unlikely, could there be any environment setting that causes my imports to be overridden ? Any pointers on what's been done wrong here would be of great help.

Thanks.

1
  • Do you have jaxb impl jar in your classpath. If yes, then remove it because jaxb classes should be bunlded in your jdk library (from java 6). Commented Dec 10, 2012 at 9:54

1 Answer 1

2

In your example the jaxb.properties file must be in the same package as your Config class.

Once you solve your current issue, the following import is going to cause you problems. JAXBContext.newInstance(Config.class) is going to return you an instance of javax.xml.bind.JAXBContext so you should import that instead.

import org.eclipse.persistence.jaxb.JAXBContext;

Below is a link to an example of GitHub that leverages a jaxb.properties file to specify MOXy as the JAXB provider.

For More Information

Note:

The current version of EclipseLink is 2.4.1, you can download it from the following location:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.