Glassfish 3.1.2.2 Web Profile and EclipseLink MOXy
I’ve been evaluating Glassfish to possibly replace Tomcat as our servlet container of choice at work. Because one of the things I love about Tomcat is that it’s so light-weight I read the list of included features in Glassfish Full Profile with a growing sense of dread.
Our .war files only want a basic Servlet environment that provides a database link - they use this to expose RESTful resources and services. Because of this, the Glassfish Web Profile seemed to suit our needs best - all the niceness of multiple domains & a more robust automated deployment without the word “Enterprise” being mentioned too often.
MOXy Trouble#
So I took one of our applications and deployed it in Glassfish. But when our code tried to initialise an EclipseLink MOXy JAXBContext we got:
java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
...
at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.JAXBException not found by org.eclipse.persistence.moxy [156]
at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1460)
at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:72)
at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1843)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 31 more
Hmm, not so good! I did some digging and discovered the problem was a combination of factors:
- Glassfish Web Profile doesn’t include a JAXB implementation
- Because it has no JAXB implementation it falls back on the JAXB implementation in the Java environment
- For OSGI purposes, it assumes the version of JAXB provided by the environment is “0.0.0”
- EclipseLink MOXy (helpfully included in Glassfish Web Profile) requires version 2.0.0 of javax.bind.xml
So unfortunately the version of JAXB available to MOXy is 0.0.0 but it needs 2.0.0.
The Solution#
The solution to this problem turned out to be pretty simple - simply copy the JAXB implementation included in the Glassfish Full Profile into the lighter Web Profile installation:
cp jaxb-osgi.jar ~/glassfish/glassfish/modules/cp jaxb-api-osgi.jar ~/glassfish/glassfish/modules/endorsed/
I actually had an additional step - the latest MOXy libraries (2.4.1) have a bug that affects our software and currently necessitates we use a snapshot release - so I replaced glassfish/modules/org.eclipse.persistence.* with the 2.4.2-SNAPSHOT jars. Restart the domain and hey presto, no exceptions.