Com.ctc.stax.WstxOutputFactory cannot be cast

Hi, I am trying to use -

final XMLOutputFactory2 xmlOutputFactory = (XMLOutputFactory2) XMLOutputFactory2.newInstance();//XMLOutputFactory.newInstance();
final XMLStreamWriter2 xmlStreamWriter = (XMLStreamWriter2) xmlOutputFactory.createXMLStreamWriter(new FileWriter(file));

Error I am getting -

Caused by: java.lang.ClassCastException: com.ctc.wstx.stax.WstxOutputFactory cannot be cast to javax.xml.stream.XMLtputFactory

on line 1.

I have gone through -



https://jira.atlassian.com/browse/JRASERVER-26214

did not really understand any of them or what I am supposed to change.

Specific condition when I am getting this error -
When I am running my code in Http thread(i.e) the main thread code works fine, but when I am using a worker thread this error is thrown.

My pom.xml -

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>
        <dependency>
            <groupId>com.ctc.wstx</groupId>
            <artifactId>woodstox-osgi</artifactId>
            <version>3.2.1.1</version>
        </dependency>

As I have told earlier that I only get error when I run this code asynchronously (i.e worker thread) and it was working fine on main thread. I worked around the code using class loader from main thread. I am not sure if that is a good thing or not. Can anyone tell me if the workaround is not messing something else.

        final ClassLoader original = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader(classLoader);
        final XMLOutputFactory2 xmlOutputFactory = (XMLOutputFactory2) XMLOutputFactory.newInstance();
        Thread.currentThread().setContextClassLoader(original);