Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot use docx4j inside of JBoss 7

I was able to successfully create a simple project with docx4j ( http://www.docx4java.org ). This simple project successfully created and wrote on a .docx document. Now I am trying to accomplish the same thing inside of a bigger project. This bigger project is a webapp running in JBoss AS 7.1. However, at my first docx4j related line of code:

WordprocessingMLPackage wmlp = WordprocessingMLPackage.createPackage();

It runs into an error:

java.lang.NoClassDefFoundError: Could not initialize class org.docx4j.jaxb.Context
    at org.docx4j.openpackaging.parts.JaxbXmlPart.<init>(JaxbXmlPart.java:79) [docx4j-2.8.1.jar:]
    at org.docx4j.openpackaging.parts.JaxbXmlPartXPathAware.<init>(JaxbXmlPartXPathAware.java:64) [docx4j-2.8.1.jar:]
    at org.docx4j.openpackaging.parts.WordprocessingML.DocumentPart.<init>(DocumentPart.java:157) [docx4j-2.8.1.jar:]
    at org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart.<init>(MainDocumentPart.java:76) [docx4j-2.8.1.jar:]
    at org.docx4j.openpackaging.packages.WordprocessingMLPackage.createPackage(WordprocessingMLPackage.java:432) [docx4j-2.8.1.jar:]
    at org.docx4j.openpackaging.packages.WordprocessingMLPackage.createPackage(WordprocessingMLPackage.java:421) [docx4j-2.8.1.jar:]
    at foo.servlets.bar.DocxServlet.doGet(DocxServlet.java:101) [classes:]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:734) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
    at foo.includes.other.ServletNameSetFilter.doFilter(ServletNameSetFilter.java:83) [fooClasses.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
    at foo.includes.other.HtmlEscapeFilter.doFilter(HtmlEscapeFilter.java:67) [fooClasses.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
    at foo.includes.other.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:64) [fooClasses.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
    at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.ajp.AjpProcessor.process(AjpProcessor.java:505) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.ajp.AjpProtocol$AjpConnectionHandler.process(AjpProtocol.java:445) [jbossweb-7.0.13.Final.jar:]
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
    at java.lang.Thread.run(Thread.java:636) [rt.jar:1.6.0_20]

Also, when I stop and restart my server without rebuilding my EAR, this error doesn't show up and instead I get a NoSuchElementException from the same method call (createPackage()):

I believe it has to do with the fact that JBoss has its own JAXB implementation, but JAXBContext is initialized within the docx4j library. Here is the code (from http://www.docx4java.org/trac/docx4j/browser/trunk/docx4j/src/main/java/org/docx4j/jaxb/Context.java):

jc = JAXBContext.newInstance("org.docx4j.wml:" +
                "org.docx4j.dml:org.docx4j.dml.chart:org.docx4j.dml.chartDrawing:org.docx4j.dml.compatibility:org.docx4j.dml.diagram:org.docx4j.dml.lockedCanvas:org.docx4j.dml.picture:org.docx4j.dml.wordprocessingDrawing:org.docx4j.dml.spreadsheetdrawing:org.docx4j.dml.diagram2008:" +
                // All VML stuff is here, since compiling it requires WML and DML (and MathML), but not PML or SML
                "org.docx4j.vml:org.docx4j.vml.officedrawing:org.docx4j.vml.wordprocessingDrawing:org.docx4j.vml.presentationDrawing:org.docx4j.vml.spreadsheetDrawing:org.docx4j.vml.root:" +
                "org.opendope.xpaths:org.opendope.conditions:org.opendope.questions:org.opendope.components:org.opendope.SmartArt.dataHierarchy:" +
                "org.docx4j.math:" +
                "org.docx4j.sharedtypes:org.docx4j.bibliography",classLoader );
log.info("loaded " + jc.getClass().getName() + " .. loading others ..");

jcThemePart = jc; //JAXBContext.newInstance("org.docx4j.dml",classLoader );
jcDocPropsCore = JAXBContext.newInstance("org.docx4j.docProps.core:org.docx4j.docProps.core.dc.elements:org.docx4j.docProps.core.dc.terms",classLoader );
jcDocPropsCustom = JAXBContext.newInstance("org.docx4j.docProps.custom",classLoader );
jcDocPropsExtended = JAXBContext.newInstance("org.docx4j.docProps.extended",classLoader );
jcXmlPackage = JAXBContext.newInstance("org.docx4j.xmlPackage",classLoader );
jcRelationships = JAXBContext.newInstance("org.docx4j.relationships",classLoader );
jcCustomXmlProperties = JAXBContext.newInstance("org.docx4j.customXmlProperties",classLoader );
jcContentTypes = JAXBContext.newInstance("org.docx4j.openpackaging.contenttype",classLoader );

jcSectionModel = JAXBContext.newInstance("org.docx4j.model.structure.jaxb",classLoader );

jcXmlDSig = JAXBContext.newInstance("org.plutext.jaxb.xmldsig",classLoader );

From what I have observed, it seems that I might need to put a jaxb.properties file specifying which jaxb implementation to use in every package listed in each JAXBContext instance above. Is this correct? Is there anything I am missing?

like image 597
user633286 Avatar asked Apr 25 '13 00:04

user633286


4 Answers

In Maven pom.xml place these missing dependencies and the problem was solved (on JBoss 7.1.3)

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.6.1</version>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.2.7</version>
</dependency>
<dependency>
    <groupId>com.googlecode.jaxb-namespaceprefixmapper-interfaces</groupId>
    <artifactId>JAXBNamespacePrefixMapper</artifactId>
    <version>2.2.4</version>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>xerces</groupId>
    <artifactId>xercesImpl</artifactId>
    <version>2.11.0</version>
</dependency>
like image 67
juanjogoa Avatar answered Nov 18 '22 05:11

juanjogoa


JBoss AS7 has a class loading mechanism which is different from previous versions. See generally https://docs.jboss.org/author/display/AS71/Class+Loading+in+AS7

It includes a module named javax.xml.bind.api; to get docx4j working in your WAR, you just need to include WEB-INF/jboss-deployment-structure.xml containing:

<deployment>
    <dependencies>

        <module name="com.sun.xml.bind" />

    </dependencies>
</deployment>

Details/discussion

modules\javax\xml\bind\api\main says:

<dependencies>
    <module name="javax.activation.api" export="true"/>
    <module name="javax.xml.stream.api"/>
    <module name="com.sun.xml.bind" services="import"/>
    <module name="javax.api"/>
</dependencies>

<resources>
    <resource-root path="jboss-jaxb-api_2.2_spec-1.0.3.Final.jar"/>
    <!-- Insert resources here -->
</resources>

modules\com\sun\xml\bind\main says:

<resources>
    <resource-root path="jaxb-impl-2.2.4.jar"/>
    <resource-root path="jaxb-xjc-2.2.4.jar"/>
    <!-- Insert resources here -->
</resources>

<dependencies>
    <module name="javax.api" />
    <module name="javax.xml.bind.api" />
    <module name="javax.xml.stream.api" />
</dependencies>

so you might think the following would also work:

<deployment>
    <dependencies>

        <module name="javax.xml.bind.api" />

    </dependencies>
</deployment>

but it didn't seem to, perhaps because is given effect?

like image 23
JasonPlutext Avatar answered Nov 18 '22 04:11

JasonPlutext


I assume you are using Java 7. I was able to solve this issue with JBoss EAP 6.2 running on

java version "1.7.0_55"
OpenJDK Runtime Environment (IcedTea 2.4.7) (7u55-2.4.7-1ubuntu1~0.13.10.1)
OpenJDK 64-Bit Server VM (build 24.51-b03, mixed mode)
  1. Define following JBoss module:

    <!-- module.xml content -->
    <module xmlns="urn:jboss:module:1.1" name="org.docx4j-compat">
        <dependencies>
            <module name="sun.jdk"/>
            <system export="true">
                <paths>
                    <path name="com/sun/xml/internal/bind/marshaller"/>
                </paths>
            </system>
        </dependencies>
    </module>
    
  2. Add dependency on this module to your WEB-INF/jboss-deployment-structure.xml.

like image 2
Illya Kysil Avatar answered Nov 18 '22 05:11

Illya Kysil


So after some research we find the solution for docx4j will work on jboss

u need to define new module in the jboss lets call it org.docx4j-compat

and it will be at \system\layers\base\org

now add all the relevant jars to the main folder and the module.xml

the module.xml :

<resources>
    <resource-root path="docx4j-3.1.0.jar"/>
    <resource-root path="docx4j-ImportXHTML-3.0.1.jar"/> 
    <resource-root path="jaxb-api-2.1.jar"/>
    <resource-root path="jaxb-svg11-1.0.2.jar"/>
    <resource-root path="jaxb-xmldsig-core-1.0.0.jar"/>
    <resource-root path="jaxb-xslfo-1.0.1.jar"/>
</resources>

<dependencies>
    <module name="org.slf4j"/>
    <module name="org.apache.commons.io"/>
    <module name="sun.jdk"/>
    <system export="true">
        <paths>
            <path name="com/sun/xml/internal/bind/marshaller"/>
        </paths>
    </system>
    <module name="javax.xml.bind.api"/>
    <module name="javax.api"/>
    <module name="com.sun.xml.bind" />
</dependencies>

hope it was helpful :)

like image 1
Moshe Latin Avatar answered Nov 18 '22 05:11

Moshe Latin