Cannot use docx4j inside of JBoss 7
Asked Answered
A

6

5

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?

Azoic answered 25/4, 2013 at 0:46 Comment(1)
I have the same issue but in tomcat and in 2019 and the solutions below do not work for me :/Juggins
V
4

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>
Varix answered 28/8, 2014 at 13:23 Comment(2)
Please post in EnglishBlink
After try the other answers this works for me on JBoss EAP 6.0.1 - JDK 7Bermuda
S
3

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?

Specter answered 15/10, 2013 at 0:28 Comment(0)
B
2

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.

Briony answered 19/6, 2014 at 7:50 Comment(0)
V
1

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 :)

Volin answered 24/8, 2014 at 13:0 Comment(0)
S
0

Please have a look at the docx4j JBOSS deployment forum

For example, from one of the posts:

version of my Serializer and Xalan jars. I had 2.7.1 for each as instructed, but my App Server (JBoss 5.1.0) had different versions (un-numbered: serializer.jar and xalan.jar) in the root lib/endorsed folder which was overriding these. When i replaced these jars with the 2.7.1 versions it worked perfectly.

Please let us know if the suggestions there work for you, or not.

Specter answered 25/4, 2013 at 1:26 Comment(1)
I get the same error, I tried your soulution but it did not work, same error.Bromate
D
0

Those two minimal steps worked for me (JBOSS 7.1.3, DOCX4J 3.2.2):

  • Configure the `WEB-INF\jboss-deployment-structure.xml` file
    <?xml version="1.0" encoding="UTF-8"?>
    <jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
        <deployment>    
            <dependencies>
                <module name="com.sun.xml.bind" />
            </dependencies>
        </deployment>
    </jboss-deployment-structure>
    
  • Modify my pom.xml
            <dependency>
                <groupId>com.googlecode.jaxb-namespaceprefixmapper-interfaces</groupId>
                <artifactId>JAXBNamespacePrefixMapper</artifactId>
                <version>2.2.4</version>
                <scope>runtime</scope>
            </dependency>
    
  • Despot answered 11/3, 2016 at 11:42 Comment(0)

    © 2022 - 2024 — McMap. All rights reserved.