Search code examples
javaxmleclipsejaxbmoxy

MOXy Marshalling/Unmarshalling error: javax.xml.bind.PropertyException due to wrong JAXBContextFactory being used


I've been trying to marshal/unmarshal some JSON and XML files for my project using Eclipse MOXy. After a set of tests I ran into javax.xml.bind.PropertyException and it seems that my jaxb.properties file isn't being read and therefore the JAXBContextFactory being used is wrong.

I am using Eclipselink (MOXy) 2.5.2. I have added jaxb.properties to the same folder where my class is, with the following content:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

And this is my test class:

public static void main(String[] args) throws Exception {
        JAXBContext jaxbContext = JAXBContext.newInstance(SIRFCatalog.class);
        System.out.println(jaxbContext.getClass());
    //  SIRFCatalog catalog;
    //  Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

    // ** The exception is thrown in the line below:**
    //  jaxbUnmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
    //  jaxbUnmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
    //  FileInputStream fis = new FileInputStream(new File("/home/philviana/a.json"));
    //  catalog = (SIRFCatalog) jaxbUnmarshaller.unmarshal(fis);

}

When I run the test above (please see that the useful code is commented out), I get the following output:

class com.sun.xml.bind.v2.runtime.JAXBContextImpl

which indicates that MOXy's JAXB context factory isn't being lodead. So in other words, I want to use org.eclipse.persistence.jaxb.JAXBContextFactory as my JAXBContextFactory (comes from MOXy) but instead I'm getting the RI's class com.sun.xml.bind.v2.runtime.JAXBContextImpl.

The jaxb.properties file is under the same directory as my class with the main method (only class of my project). These are my JARs:

eclipselink.jar
com.sun.tools.xjc_2.2.0.jar
com.sun.xml.bind_2.2.0.v201004141950.jar
javax.activation_1.1.0.v201108011116.jar
javax.mail_1.4.0.v201005080615.jar
javax.xml.bind_2.2.0.v201105210648.jar
javax.xml.stream_1.0.1.v201004272200.jar
org.eclipse.persistence.antlr-2.5.2.jar
org.eclipse.persistence.asm-2.5.2.jar
org.eclipse.persistence.core-2.5.2.jar
org.eclipse.persistence.moxy-2.5.2.jar
javax.persistence.source_2.1.0.v201304241213.jar
javax.persistence_2.1.0.v201304241213.jar
org.eclipse.persistence.jpa.modelgen.source_2.5.2.v20140319-9ad6abd.jar
org.eclipse.persistence.jpa.modelgen_2.5.2.v20140319-9ad6abd.jar
org.eclipse.persistence.jpars.source_2.5.2.v20140319-9ad6abd.jar
org.eclipse.persistence.jpars_2.5.2.v20140319-9ad6abd.jar
commonj.sdo_2.1.1.v201112051852.jar

Any thougts?


Solution

  • You need to ensure that the jaxb.properties file ends up in the classpath. If you are running it from Eclipse it is possible that the jaxb.properties is not being copied from the src directory to the directory where the classes are compiled to.