How to specify a particular JAXB implementation?

Eric B. picture Eric B. · Apr 3, 2012 · Viewed 27.2k times · Source

It seems as though I have already done this once in the past, but I cannot find any reference to what I had done to get it to work.

I have a web application in which I want to specify a different JAXB implementation than the one provided by my web server/jre. I downloaded the appropriate artifact from maven, and see that the jar is properly packaged in my war, however, when I start my web app, I see that it is still using the bundled JRE implementation.

I vaguely remember something about a properties file that I could configure, but cannot find a reference to how it needs to be configured. Moreover, if the implementation I want to use is the same one (just a newer version), the class names would be the same as though packaged in the JRE. How can I specify that I want to use the ones bundled in my WAR?

EDIT

I'm currently running on JBoss 7.0.2, with Oracle JDK 1.6_0_27, JAXB RI that comes with the JRE (I think it is v2.1). I'm trying to upgrade to JAXB RI 2.2.5 (found on MvnRepository).

I've done a little more digging this morning, and noticed a strange error message in my logs:

09:43:18,325 WARN  [org.jboss.as.server.deployment] (MSC service thread 1-12) Class Path entry jaxb-api.jar in "/C:/servers/jboss-as-7.0.2.Final/standalone/deployments/LendingSimulationServiceEAR.ear/LendingSimulationService.war/WEB-INF/lib/jaxb-impl-2.2.5.jar"  does not point to a valid jar for a Class-Path reference.
09:43:18,325 WARN  [org.jboss.as.server.deployment] (MSC service thread 1-12) Class Path entry activation.jar in "/C:/servers/jboss-as-7.0.2.Final/standalone/deployments/LendingSimulationServiceEAR.ear/LendingSimulationService.war/WEB-INF/lib/jaxb-impl-2.2.5.jar"  does not point to a valid jar for a Class-Path reference.
09:43:18,326 WARN  [org.jboss.as.server.deployment] (MSC service thread 1-12) Class Path entry jsr173_1.0_api.jar in "/C:/servers/jboss-as-7.0.2.Final/standalone/deployments/LendingSimulationServiceEAR.ear/LendingSimulationService.war/WEB-INF/lib/jaxb-impl-2.2.5.jar"  does not point to a valid jar for a Class-Path reference.
09:43:18,326 WARN  [org.jboss.as.server.deployment] (MSC service thread 1-12) Class Path entry jaxb1-impl.jar in "/C:/servers/jboss-as-7.0.2.Final/standalone/deployments/LendingSimulationServiceEAR.ear/LendingSimulationService.war/WEB-INF/lib/jaxb-impl-2.2.5.jar"  does not point to a valid jar for a Class-Path reference.

Which I found very strange. I wasn't sure where it was finding that information. A little more research found this line in the MANIFEST.MF:

Class-Path: jaxb-api.jar activation.jar jsr173_1.0_api.jar jaxb1-impl.jar

So now I'm even more confused than ever. It would appear that the jaxb implementation depends on the api, activation, jsr and jaxb1 implementation jars. But they aren't listed in the jaxb pom. A little digging online found me this link which discusses how to use JAXB 2.2 in a Java6SE environment. Unfortunately, this has not seemed to work either; I still get the above WARN messages.

I'm using the following snippet to list the JAXB implementation that is running; perhaps this is incorrect?

/**
 * Print the JAXB Implementation information
 */
public static void outputJaxpImplementationInfo() {
    logger.debug(getImplementationInfo("DocumentBuilderFactory", DocumentBuilderFactory.newInstance().getClass()));
    logger.debug(getImplementationInfo("XPathFactory", XPathFactory.newInstance().getClass()));
    logger.debug(getImplementationInfo("TransformerFactory", TransformerFactory.newInstance().getClass()));
    logger.debug(getImplementationInfo("SAXParserFactory", SAXParserFactory.newInstance().getClass()));
}

/**
 * Get the JAXB implementation information for a particular class
 * @param componentName
 * @param componentClass
 * @return
 */
private static String getImplementationInfo(String componentName, Class componentClass) {
    CodeSource source = componentClass.getProtectionDomain().getCodeSource();
    return MessageFormat.format(
            "{0} implementation: {1} loaded from: {2}",
            componentName,
            componentClass.getName(),
            source == null ? "Java Runtime" : source.getLocation());
}

This snippet produces the following log:

10:28:27,402 INFO  [stdout] (MSC service thread 1-14) 2012-04-04 10:28:27,402 DEBUG cws.cs.lendingsimulationservice.util.JAXBUtil  - DocumentBuilderFactory implementation: __redirected.__DocumentBuilderFactory loaded from: file:/C:/servers/jboss-as-7.0.2.Final/jboss-modules.jar
10:28:27,403 INFO  [stdout] (MSC service thread 1-14) 2012-04-04 10:28:27,403 DEBUG cws.cs.lendingsimulationservice.util.JAXBUtil  - XPathFactory implementation: __redirected.__XPathFactory loaded from: file:/C:/servers/jboss-as-7.0.2.Final/jboss-modules.jar
10:28:27,404 INFO  [stdout] (MSC service thread 1-14) 2012-04-04 10:28:27,404 DEBUG cws.cs.lendingsimulationservice.util.JAXBUtil  - TransformerFactory implementation: __redirected.__TransformerFactory loaded from: file:/C:/servers/jboss-as-7.0.2.Final/jboss-modules.jar
10:28:27,406 INFO  [stdout] (MSC service thread 1-14) 2012-04-04 10:28:27,406 DEBUG cws.cs.lendingsimulationservice.util.JAXBUtil  - SAXParserFactory implementation: __redirected.__SAXParserFactory loaded from: file:/C:/servers/jboss-as-7.0.2.Final/jboss-modules.jar

Answer

bdoughan picture bdoughan · Apr 4, 2012

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.

To specify a JAXB (JSR-222) implementation other than the default you need to include a file called jaxb.properties in the same package as your domain classes. The following is an example of the jaxb.properties file used to specify the MOXy implementation of JAXB:

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

For More Information