Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache POI Workbook throws java.lang.reflect.InvocationTargetException

I am trying to instantiate a Workbook with this code:

Workbook workbook = WorkbookFactory.create(new File(excelWorkbookFilePath));

I do it this way because the workbook can either be .xlsx or .xls. However, it ends up throwing this error:

Exception in thread "main" org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException
    at org.apache.poi.POIXMLFactory.createDocumentPart(POIXMLFactory.java:65)
    at org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:601)
    at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:174)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:249)
    at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:293)
    at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:252)
    at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:231)
    at com.iconectiv.handle.ExcelHandle.<init>(ExcelHandle.java:43)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:56)
    at org.apache.poi.POIXMLFactory.createDocumentPart(POIXMLFactory.java:62)
    ... 9 more
Caused by: java.lang.NoSuchMethodError: org.apache.xmlbeans.XmlOptions.setLoadEntityBytesLimit(I)Lorg/apache/xmlbeans/XmlOptions;
    at org.apache.poi.POIXMLTypeLoader.<clinit>(POIXMLTypeLoader.java:50)
    at org.apache.poi.xssf.model.ThemesTable.<init>(ThemesTable.java:85)
    ... 15 more

I did some research and found out that InvocationTargetException is not the real exception, so I tried to catch it with this catch statement:

} catch (InvocationTargetException e) {
    e.getCause().printStackTrace();
}

However, I get an error in Eclipse saying that I can't catch that as it is never thrown from anything. Why am I getting this error? Any help would be appreciated, thanks in advance!

Note: I am using Apache POI 3.14.

like image 852
o.o Avatar asked Dec 18 '25 22:12

o.o


1 Answers

The actual error getting thrown is NoSuchMethodError: org.apache.xmlbeans.XmlOptions.setLoadEntityBytesLimit(I)Lorg/apache/xmlbeans/XmlOptions;

Possibility you don't have xmlbean jar in your class path or you might have multiple version of jars

like image 191
VGaur Avatar answered Dec 20 '25 16:12

VGaur



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!