Warrior
Warrior

Reputation: 3304

Exception in thread "main" java.lang.NoSuchFieldError: Factory

Recently upgraded POI jar version from 3.17 to 5.1 and below code which was working in 3.x is now broken, ( jdk 1.8 )

Below are the set of Jar used enter image description here

below is my stub:

import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadExcelDemo 
{
    public static void main(String[] args) 
    {
        try
        {
            FileInputStream file = new FileInputStream(new File("D:\\TemporaryUploadContainer\\61C50E5C2395C7FB48499BCFDD797F15_QuickCartExample.xlsx"));
 
            //Workbook workbook1 = WorkbookFactory.create(file);
            //Sheet sheet1 = workbook1.getSheetAt(0);
                        
            XSSFWorkbook workbook = new XSSFWorkbook(file);
            XSSFSheet sheet = workbook.getSheetAt(0);
 
            //Iterate through each rows one by one
            Iterator<Row> rowIterator = sheet.iterator();
            while (rowIterator.hasNext()) 
            {
                Row row = rowIterator.next();
                //For each row, iterate through all the columns
                Iterator<Cell> cellIterator = row.cellIterator();
                 
                while (cellIterator.hasNext()) 
                {
                    Cell cell = cellIterator.next();
                    //Check the cell type and format accordingly
                    switch (cell.getCellType()) 
                    {
                        case NUMERIC:
                            System.out.print(cell.getNumericCellValue() + "t");
                            break;
                        case STRING:
                            System.out.print(cell.getStringCellValue() + "t");
                            break;
                    }
                }
                System.out.println("");
            }
            file.close();
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }
}

Below Error for : Workbook workbook1 = WorkbookFactory.create(file);

Exception in thread "main" java.lang.NoSuchFieldError: Factory at org.apache.poi.xssf.model.ThemesTable.(ThemesTable.java:86) at org.apache.poi.ooxml.POIXMLFactory.createDocumentPart(POIXMLFactory.java:61) at org.apache.poi.ooxml.POIXMLDocumentPart.read(POIXMLDocumentPart.java:661) at org.apache.poi.ooxml.POIXMLDocument.load(POIXMLDocument.java:165) at org.apache.poi.xssf.usermodel.XSSFWorkbook.(XSSFWorkbook.java:275) at org.apache.poi.xssf.usermodel.XSSFWorkbook.(XSSFWorkbook.java:296) at test.XLSXReaderWriterP21.readRecordsFromFile(XLSXReaderWriterP21.java:76) at test.XLSXReaderWriterP21.main(XLSXReaderWriterP21.java:57)

Below Error for : XSSFWorkbook workbook = new XSSFWorkbook(file);

Exception in thread "main" java.lang.NoSuchFieldError: Factory at org.apache.poi.xssf.model.ThemesTable.(ThemesTable.java:86) at org.apache.poi.ooxml.POIXMLFactory.createDocumentPart(POIXMLFactory.java:61) at org.apache.poi.ooxml.POIXMLDocumentPart.read(POIXMLDocumentPart.java:661) at org.apache.poi.ooxml.POIXMLDocument.load(POIXMLDocument.java:165) at org.apache.poi.xssf.usermodel.XSSFWorkbook.(XSSFWorkbook.java:275) at org.apache.poi.xssf.usermodel.XSSFWorkbook.(XSSFWorkbook.java:296) at test.ReadExcelDemo.main(ReadExcelDemo.java:26)

Upvotes: 13

Views: 28499

Answers (5)

Frantzisko
Frantzisko

Reputation: 31

I had the same issue after updating org.apache.poi:poi-ooxml and org.apache.poi:poi-ooxml from version 4.1.2 to 5.2.2.

That's because com.monitorjbl:xlsx-streamer:2.1.0 was bringing poi-ooxml-schemas-4.1.2. as a dependency.

So I've updated the outdated com.monitorjbl:xlsx-streamer:2.1.0 with its fork com.github.pjfanning:excel-streaming-reader:4.0.4:

  <dependency>
    <groupId>com.github.pjfanning</groupId>
    <artifactId>excel-streaming-reader</artifactId>
    <version>4.0.4</version>
  </dependency>

Upvotes: 0

Giplo
Giplo

Reputation: 229

I had the same problem. Try to remove poi-ooxml-schemas-4.1.2.jar, and add poi-ooxml-lite-5.2.1.jar

Upvotes: 22

natan barron
natan barron

Reputation: 61

I had the same problem with Netbeans, when i try to use:

XSSFWorkbook workbook = new XSSFWorkbook(fis);

or

Workbook workbook = WorkbookFactory.create(inputFile);

I fixed the problem changing poi-ooxml-schemas-4.1.2.jar for poi-ooxml-lite-5.2.2.jar

Upvotes: 5

Maik Costa
Maik Costa

Reputation: 25

I think you had only adjust to same version this poi conform this FAQ https://poi.apache.org/help/faq.html#faq-N10204

Upvotes: 0

stavromachinima
stavromachinima

Reputation: 1

Hey just had the same problem as you. Got it resolved by adding the following dependencies.

poi-4.1.2.jar poi-ooxml-4.1.2.jar poi-ooxml-schemas-4.1.2.jar xmlbeans-3.1.0.jar

As the other comment has suggested, poi-jars should be the same version, else it won't work correctly.

Upvotes: -1

Related Questions