Busuioc Razvan
Busuioc Razvan

Reputation: 23

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

I'm trying to test a xml to excel converter in Java and I have the following exception

Exception in thread "main" java.lang.NoSuchFieldError: Factory
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.onWorkbookCreate(XSSFWorkbook.java:475)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:232)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:226)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:214)
    at xmlToExcel.Converter.initXls(Converter.java:135)
    at xmlToExcel.Converter.getAndReadXml(Converter.java:60)
    at xmlToExcel.Converter.main(Converter.java:36)

The exception refers to the this line:

  workbook = new XSSFWorkbook();

And here are my current jars

enter image description here

How can I solve this? Are there any conflicts in the jars?

EDIT 1: Here is the code for the method which initialises the POI workbook and writes the header row

    workbook = new XSSFWorkbook();

    CellStyle style = workbook.createCellStyle();
    Font boldFont = workbook.createFont();
    boldFont.setBold(true);
    style.setFont(boldFont);
    style.setAlignment(HorizontalAlignment.CENTER);

    Sheet sheet = workbook.createSheet();
    rowNum = 0;
    Row row = sheet.createRow(rowNum++);
    Cell cell = row.createCell(SUBSTANCE_NAME_COLUMN);
    cell.setCellValue("Substance name");
    cell.setCellStyle(style);

The exception references the first line, the creation of the workbook.

Upvotes: 2

Views: 7110

Answers (1)

Axel Richter
Axel Richter

Reputation: 61870

While using apache poi never mix jars from different versions. See faq-N10204.

So when used apache poi version 5.2.1 all apache poi jars needs to be from that version. So in your case apache poi 5.2.1 cannot be used together with poi-ooxml-schemas-4.1.2.jar. It needs to be used with either poi-ooxml-lite-5.2.1.jar or poi-ooxml-full-5.2.1.jar.

To avoid conflicts, furthermore all jars from not matching versions should be excluded from class path while running apache poi code.

So in your case using apache poi 5.2.1 there should not be any poi-ooxml-schemas-4.1.2.jar in class path additional to either poi-ooxml-lite-5.2.1.jar or poi-ooxml-full-5.2.1.jar. Also ooxml-schemas-1.4.jar should not be in class path while using apache poi 5. The ooxml-schemas-1.4.jar was the former version of poi-ooxml-full-5.*.jar and also cannot be used together with apache poi 5.

Upvotes: 6

Related Questions