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.