Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
290 views
in Technique[技术] by (71.8m points)

jboss7.x - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...