Problem with docx4j in jira plugin

Rbig December 7, 2017

I have this error in jira : 

java.lang.NoClassDefFoundError: Could not initialize class org.docx4j.openpackaging.parts.DocPropsCorePart

and in my console log i have this error :  

[INFO] [talledLocalContainer] 2017-12-07 11:26:39,714 http-bio-2990-exec-14 WARN admin 686x504x1 1fzlz83 127.0.0.1 /secure/docxManager.jspa [docx4j.openpackaging.parts.JaxbXmlPartXPathAware] .. and mark not supported

[INFO] [talledLocalContainer] 2017-12-07 11:26:39,715 http-bio-2990-exec-14 ERROR admin 686x504x1 1fzlz83 127.0.0.1 /secure/docxManager.jspa [docx4j.openpackaging.packages.WordprocessingMLPackage] com.ctc.wstx.stax.WstxInputFactory cannot be cast to javax.xml.stream.XMLInputFactory

[INFO] [talledLocalContainer] javax.xml.bind.JAXBException: com.ctc.wstx.stax.WstxInputFactory cannot be cast to javax.xml.stream.XMLInputFactory

[INFO] [talledLocalContainer] - with linked exception:

[INFO] [talledLocalContainer] [java.lang.ClassCastException: com.ctc.wstx.stax.WstxInputFactory cannot be cast to javax.xml.stream.XMLInputFactory]

 

My class docx4j : 

public class DocxManager extends JiraWebActionSupport {

private static Logger log = LoggerFactory.getLogger(ReleasePlugin.class);
private ActiveObjects ao;
private IssueManager issueManager;

public DocxManager(ActiveObjects ao) {
try {
this.ao = ao;
this.issueManager = ComponentManager.getInstance().getIssueManager();
} catch (Exception e) {
e.printStackTrace();
log.error("Erreur getProjectNames:" + e.getMessage());
}
}

public String doExecute(){
try {
String xhtml =
"<table><tr><th colspan=\"3\" style=\"border:1px solid #ddd;padding:8px;padding-top:12px;padding-bottom:12px;text-align:left;background-color:#dec934;text-align:center;\">IDENTITÉ DU DOCUMENT</th></tr><tr><td style=\"border:1px solid #ddd;padding:8px;\"><strong>Type de document</strong></td><td style=\"border: 1px solid #ddd;padding:8px;\">Note de publication -- version progicielle ...</td></tr><tr><td style=\"border: 1px solid #ddd; padding: 8px;\"><strong>État</strong></td><td style=\"border: 1px solid #ddd;padding:8px;\">Final - V1.0</td></tr><tr><td style=\"border: 1px solid #ddd;padding:8px;\"><strong>Progiciel</strong></td><td style=\"border:1px solid #ddd;padding:8px;\">TEST</td></tr><tr><td style=\"border:1px solid #ddd;padding:8px;\"><strong>Version progicielle</strong></td><td style=\"border:1px solid #ddd;padding:8px;\">2017 ...</td></tr><tr><td><strong> Date de version </strong></td><td>08/11/2017</td></tr></table>";
// To docx, with content controls
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
log.error("test1");
XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
// XHTMLImporter.setDivHandler(new DivToSdt());
log.error("test2");

wordMLPackage.getMainDocumentPart().getContent().addAll(
XHTMLImporter.convert(xhtml, null));
wordMLPackage.save(new File("./OUT_from_XHTML.docx"));
log.error("test3");

System.out.println(XmlUtils.marshaltoString(wordMLPackage
.getMainDocumentPart().getJaxbElement(), true, true));

// wordMLPackage.save(new java.io.File(System.getProperty("user.dir")
// + "/OUT_from_XHTML.docx"));

// Back to XHTML

HTMLSettings htmlSettings = Docx4J.createHTMLSettings();
htmlSettings.setWmlPackage(wordMLPackage);


// output to an OutputStream.
OutputStream os = new ByteArrayOutputStream();

// If you want XHTML output
Docx4jProperties.setProperty("docx4j.Convert.Out.HTML.OutputMethodXML", true);
Docx4J.toHTML(htmlSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);

System.out.println(((ByteArrayOutputStream) os).toString());


} catch (Exception e) {
e.printStackTrace();
}
return null;
}

}

 

 pom.xml :

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<name>jira-version-plugin</name>
<dependencies>
<dependency>
<groupId>com.atlassian.jira</groupId>
<artifactId>jira-api</artifactId>
<version>${jira.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.atlassian.jira</groupId>
<artifactId>jira-core</artifactId>
<version>${jira.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-ImportXHTML</artifactId>
<version>3.3.6</version>
</dependency>
<dependency>
<groupId>com.atlassian.activeobjects</groupId>
<artifactId>activeobjects-plugin</artifactId>
<version>0.23.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.13</version>
</dependency>
<!--<dependency>-->
<!--<groupId>javax.servlet</ groupId>-->
<!--<artifactId>servlet-api</ artifactId>-->
<!--<version>2.4</ version>-->
<!--<scope>fourni</ scope>-->
<!--</ dependency>-->
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.2-atlassian-1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.8.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j</artifactId>
<version>3.3.6</version>
<exclusions>
<exclusion>
<groupId>javax.xml.stream</groupId>
<artifactId>stax-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--<dependency>-->
<!--<groupId>javax.xml.stream</groupId>-->
<!--<artifactId>stax-api</artifactId>-->
<!--<version>1.0-2</version>-->
<!--</dependency>-->

<!--<dependency>-->
<!--<groupId>javax.xml.bind</groupId>-->
<!--<artifactId>jaxb-api</artifactId>-->
<!--<version>2.2.4</version>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>com.sun.xml.bind</groupId>-->
<!--<artifactId>jaxb-impl</artifactId>-->
<!--<version>2.2.11</version>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>com.sun.xml.bind</groupId>-->
<!--<artifactId>jaxb-core</artifactId>-->
<!--<version>2.2.11</version>-->
<!--</dependency>-->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.atlassian.maven.plugins</groupId>
<artifactId>maven-jira-plugin</artifactId>
<version>${amps.version}</version>
<extensions>true</extensions>
<configuration>
<productVersion>${jira.version}</productVersion>
<productDataVersion>${jira.version}</productDataVersion>
<enableQuickReload>true</enableQuickReload>
<enableFastdev>false</enableFastdev>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<jira.version>6.4.6</jira.version>
<amps.version>5.0.13</amps.version>
<plugin.testrunner.version>1.2.3</plugin.testrunner.version>
<testkit.version>5.2.26</testkit.version>
</properties>
</project>

 

I need to resolve this error before 2 days, guys i believe in you.

Thanks you in advance.

1 answer

0 votes
Mihail September 9, 2020

Of course, it's obviously too late for the original question, but at last, it works.
dependencies your would need:
{code}

<dependency>
            <groupId>com.googlecode.jaxb-namespaceprefixmapper-interfaces</groupId>
            <artifactId>JAXBNamespacePrefixMapper</artifactId>
            <version>2.2.4</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <version>2.2</version>
        </dependency>

        <dependency>
            <groupId>org.docx4j</groupId>
            <artifactId>docx4j</artifactId>
            <version>6.1.2</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.woodstox</groupId>
            <artifactId>woodstox-core</artifactId>
            <version>5.0.2</version>
        </dependency>


        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
            <!--that provided REALLY matters-->
        </dependency>


        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.1.3</version>
            <scope>provided</scope>
            <!--along with that-->
        </dependency>

{code}

 

Also:

<!-- Add package import here -->
<Import-Package>    org.springframework.osgi.*;version="0";resolution:=optional,
    org.eclipse.gemini.blueprint.*;resolution:="optional",
    org.docx4j*;resolution:="required",
    javax.ws.rs*;resolution:="required",
    javax.xml.bind*;resolution:="required",
    *;version="0";resolution:="optional"</Import-Package>

 

And usage should be like that:

ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
try{
    //...different docx4j things
}finally{
    Thread.currentThread().setContextClassLoader(currentClassLoader);
}

 

Don't know if it does matter, but I also have docx4j jar in JIRA's /lib (seems, that it does NOT matters, as long, as I don't specify dependency as provided, but...).

Problem was in conflict between docx4j and JIRA's internal javax.xml - I've experimented with new pure JIRA plugin, containing only that functionality with docx4j - to manage only absolutely required dependencies.

It is deadly late for the original question, but I hope that it would help someone, roaming the internet, looking for answer. I've faced that issue about a 6 month before, in those times, I wasn't able to handle it (and used apache poi), but now, YES.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events