Bug 62831

Summary: WorkbookFactory.create() doesn't work for File subclasses
Product: POI Reporter: Maarten Coene <maarten_coene>
Component: SS CommonAssignee: POI Developers List <dev>
Status: RESOLVED FIXED    
Severity: normal    
Priority: P2    
Version: 4.0.0-FINAL   
Target Milestone: ---   
Hardware: PC   
OS: All   

Description Maarten Coene 2018-10-17 14:04:10 UTC
Hi,

If you pass a File subclass to the org.apache.poi.ss.usermodel.WorkbookFactory.create() method, a NoSuchMethodException is thrown.

For instance:
OK: WorkbookFactory.create(new File("C:\\temp\\test.xlsx"));
ERROR: WorkbookFactory.create(new File("C:\\temp\\test.xlsx") {});

Stacktrace:
Exception in thread "main" java.io.IOException: java.lang.NoSuchMethodException: org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.createWorkbook(uz.POITest$1, boolean)
	at org.apache.poi.ss.usermodel.WorkbookFactory.createWorkbook(WorkbookFactory.java:329)
	at org.apache.poi.ss.usermodel.WorkbookFactory.createXSSFWorkbook(WorkbookFactory.java:296)
	at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:284)
	at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:253)
	at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:234)
	at uz.POITest.main(POITest.java:67)
Caused by: java.lang.NoSuchMethodException: org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.createWorkbook(uz.POITest$1, boolean)
	at java.lang.Class.getMethod(Class.java:1786)
	at org.apache.poi.ss.usermodel.WorkbookFactory.createWorkbook(WorkbookFactory.java:313)
	... 5 more


The reason is that reflection is used within the WorkbookFactory class to find the correct createWorkbook method. This fails for subclasses of java.io.File.

A concrete use-case is the JFileChooser which (on Windows) returns subclasses of java.io.File when you select a file by double-clicking on it.

Maarten
Comment 1 Nick Burch 2018-10-17 14:25:20 UTC
Fixed in r1844116, thanks for tracking this one down!
Comment 2 Maarten Coene 2018-10-17 14:55:45 UTC
Thanks for the very fast reply and fix! :-D

Maarten