'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)



Solution 1:[1]

I had the same problem. Try to remove poi-ooxml-schemas-4.1.2.jar

and add

poi-ooxml-lite-5.2.1.jar

Solution 2:[2]

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

Solution 3:[3]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Giplo
Solution 2 Maik Costa
Solution 3