centic9 / poi-on-android

A sample project that shows how Apache POI can be used in an Android application
Apache License 2.0
357 stars 89 forks source link

java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/poi/javax/xml/stream/XMLStreamReader; #76

Closed ChestnutMu closed 4 years ago

ChestnutMu commented 4 years ago

I am trying to modify a docx, but I end up with the following error:

    java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/poi/javax/xml/stream/XMLStreamReader;  
    at org.apache.xmlbeans.XmlBeans.buildStreamToNodeMethod(XmlBeans.java:247)
    at org.apache.xmlbeans.XmlBeans.<clinit>(XmlBeans.java:134)
    at org.apache.xmlbeans.XmlBeans.typeLoaderForClassLoader(XmlBeans.java:715)
    at org.openxmlformats.schemas.wordprocessingml.x2006.main.DocumentDocument$Factory.getTypeLoader(Unknown Source:25)
    at org.openxmlformats.schemas.wordprocessingml.x2006.main.DocumentDocument$Factory.parse(Unknown Source:0)
    at org.apache.poi.xwpf.usermodel.XWPFDocument.onDocumentRead(XWPFDocument.java:177)
    at org.apache.poi.ooxml.POIXMLDocument.load(POIXMLDocument.java:184)
    at org.apache.poi.xwpf.usermodel.XWPFDocument.<init>(XWPFDocument.java:144)
    at cn.com.minstone.modu.component.poi.WordToHtml.createDocx(WordToHtml.java:118)
    at cn.com.minstone.modu.component.poi.PoiActivity.lambda$onCreate$2(PoiActivity.java:50)

This is my code :

    InputStream inputStream = new FileInputStream(rootPath + fileName);
    XWPFDocument xwpfDocument = new XWPFDocument(inputStream);
    List<XWPFParagraph> xwpfParagraphs = xwpfDocument.getParagraphs();
    for (XWPFParagraph xwpfParagraph : xwpfParagraphs) {
        List<XWPFRun> xwpfRuns = xwpfParagraph.getRuns();
        for (XWPFRun xwpfRun : xwpfRuns) {
            xwpfRun.setBold(false);
            xwpfRun.setColor("#FF7F24");
        }
    }
    try (OutputStream fileOut = new FileOutputStream(rootPath + fileName)) {
        xwpfDocument.write(fileOut);
    }

And I Have added this in onCrate():

    System.setProperty("org.apache.poi.javax.xml.stream.XMLInputFactory", "com.fasterxml.aalto.stax.InputFactoryImpl");
    System.setProperty("org.apache.poi.javax.xml.stream.XMLOutputFactory", "com.fasterxml.aalto.stax.OutputFactoryImpl");
    System.setProperty("org.apache.poi.javax.xml.stream.XMLEventFactory", "com.fasterxml.aalto.stax.EventFactoryImpl");
    System.setProperty("org.apache.poi.javax.xml.stream.XMLStreamReader", "com.fasterxml.aalto.stax.StreamReaderImpl");