How to specify a particular JAXB implementation?

Go To StackoverFlow.com

21

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
2012-04-03 21:56
by Eric B.
It is probably JAXB implementation dependent to some degree. Fo rinstance, the JAXB RI used to require the use of the JRE 'endorsed' mechanism if memory serve. Can you please specify the details of your environment : app server, JDK version, JAXB impl form/to to begin with - Patrice M. 2012-04-03 23:00
@PatriceM - sorry; forgot to include that. Just edited the orig question to add it in - Eric B. 2012-04-04 03:02
I edited the original post to add some additional information to indicate how I get the JAXB implementation being used, as well as a link indicating how overriding the JRE's default JAXB implementation does not seem to be working - Eric B. 2012-04-04 14:37


13

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

2012-04-03 23:19
by Blaise Doughan
Thanks for the tip; I think thats what i was trying to remember. But given the same class in both a JRE and in your war, how do you specify to use the class in the war vs the one in the jre or the providing app server - Eric B. 2012-04-04 03:04
would this have worked? compile

Compile means that you need the JAR for compiling and running the app. For a web application, as an example, the JAR will be placed in the WEB-INF/lib directory - aurelius 2015-01-21 07:52



2

It is possible to load JAXB implementation from a specific library using the Java Endorsed Standards Override Mechanism. This is also suggested in the guide Using JAXB 2.1/2.2 with JavaSE 6.

Alternatively, in your application server you may modify the class-loader policy for your application to parent last so that classes are first looked up in the libraries bundled with your application before being searched in JRE.

2013-07-22 08:21
by Somu