Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoSuchMethodError in main thread while reading xlsx using apache poi

my code is

[...]
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFRow;

public class ExcelRead {

    public static void main( String [] args ) {
        try {

              File excel = new File("Book1.xlsx");
              FileInputStream fis = new FileInputStream(excel);
              XSSFWorkbook book = new XSSFWorkbook(fis);
              XSSFSheet sheet = book.getSheetAt(0);

            Iterator rows = sheet.rowIterator(); 
            while( rows.hasNext() ) {   
                HSSFRow row = (HSSFRow) rows.next();
                System.out.println("\n");
                Iterator cells = row.cellIterator();
                while( cells.hasNext() ) {

                    HSSFCell cell = (HSSFCell) cells.next();
                    if(HSSFCell.CELL_TYPE_NUMERIC==cell.getCellType())
                    System.out.print( cell.getNumericCellValue()+"     " );
                    else
                    if(HSSFCell.CELL_TYPE_STRING==cell.getCellType())
                        System.out.print( cell.getStringCellValue()+"     " );
                    else
                        if(HSSFCell.CELL_TYPE_BOOLEAN==cell.getCellType())
                        System.out.print( cell.getBooleanCellValue()+"     " );
                        else
                            if(HSSFCell.CELL_TYPE_BLANK==cell.getCellType())
                                System.out.print( "BLANK     " );
                                else
                            System.out.print("Unknown cell type");
                }
            }
        } catch ( IOException ex ) {
            ex.printStackTrace();
        }
    }
}

the jars that i have added are

  • poi-3.9.jar
  • poi-ooxml-3.9.jar
  • poi-ooxml-schemas-3.7.jar
  • xmlbeans-2.3.0.jar
  • dom4j-1.6.1.jar

the exception is

 Exception in thread "main" java.lang.NoSuchMethodError: javax.xml.stream.XMLEventFactory.newFactory()Ljavax/xml/stream/XMLEventFactory;

      at org.apache.poi.openxml4j.opc.internal.marshallers.PackagePropertiesMarshaller.<clinit>(PackagePropertiesMarshaller.java:45)
      at org.apache.poi.openxml4j.opc.OPCPackage.init(OPCPackage.java:161)
      at org.apache.poi.openxml4j.opc.OPCPackage.<init>(OPCPackage.java:141)
      at org.apache.poi.openxml4j.opc.Package.<init>(Package.java:37)
      at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:87)
      at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:272)
      at org.apache.poi.util.PackageHelper.open(PackageHelper.java:37)
      at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:254)
      at com.symplocus.ExcelRead.main(ExcelRead.java:26)
like image 990
Rajasekhar Avatar asked Nov 11 '14 13:11

Rajasekhar


3 Answers

Recent versions of poi-ooxml require you to use at least java 6. The method in question was only added since java 6. Consider upgrading your java version.

From the official docs,

The OOXML jars require a stax implementation, but now that Apache POI requires Java 6, that is provided by the JRE and no additional stax jars are required. The OOXML jars used to require DOM4J, but the code has now been changed to use JAXP and no additional dom4j jars are required.

*Emphasis mine.

Since the question was edited, I'm specifying the build this method was added:

java 1.6.0_18

So a minimum of this version must be used in OP's case.

like image 107
mystarrocks Avatar answered Oct 23 '22 23:10

mystarrocks


This usually indicates that you somehow drag in outdated XML Parser interfaces, e.g. via some transient dependency to an old xml-api version or an outdated xerces jar, which is usually not needed any more at all as Java 6 provides all the XML parsing functionality out of the box.

See this discusion for some more details.

like image 1
centic Avatar answered Oct 23 '22 22:10

centic


I ran into exactly the same issue as you, and I found the following jar files have to be used to solve the program:

  • poi-bin-3.8.zip
  • opencsv-2.3.jar
  • poi-ooxml-3.8.jar
  • poi-ooxml-schemas-3.8.jar
  • xmlbeans-2.3.0.jar
  • dom4j-1.6.1.jar

Newer version will bring trouble.

like image 1
user4521284 Avatar answered Oct 23 '22 23:10

user4521284