Classloader problem with Java 7 and WebServices in Grails

Thursday, September 20, 2012

Our Grails 2.1 application communicates with external SOAP WebServices. It worked fine as we follow Software Guy's advices from this blog post.

Recently, our client required new functionality - export to Excel. We've used Apache POI libraries for export. And our web service communication died. All it gave us was:

Caused by: java.lang.LinkageError: loader constraint violation: when resolving field "DATETIME" the class loader (instance of org/codehaus/groovy/grails/cli/support/GrailsRootLoader) of the referring class, javax/xml/datatype/DatatypeConstants, and the class loader (instance of ) for the field's resolved type, ants, have different Class objects for that type

This message is strange, LinkageError can give you creeps. But read carefully: resolved type, ants? Something is definitely wrong here. After many searches it turned out that Java 7 already contains some conflicting classes from stax-api.jar. To solve this problem there are two thing you need to do:

  1. Ensure that your jaxws-rt dependency is runtime, not compile!
    // http://asoftwareguy.com/2012/02/25/web-service-clients-where-grails-lost-its-mojo/
    // Do not remove this dependency. Web services need this to work flawlessly.
    runtime ('com.sun.xml.ws:jaxws-rt:2.1.4')
  2. Create dependency report (grails dependency-report), search and exclude all stax-api dependencies other than jaxws-rt like this example:
    compile ('org.apache.poi:poi-ooxml-3.7') {
        excludes 'stax-api'
    }
    compile ('org.apache.poi:poi-ooxml-schemas:3.7') {
        excludes 'stax-api'
    }

2 comments: