Resource leak: workbook is never closed warning when using Apache.POI XSSFWorkbook

I had this issue, and it was making little sense. In the end I tracked the issue down to my IDE (netbeans) was picking up an earlier version of the POI libraries (v3.8) which didn't have the "close" method. So check your class path and look for duplicate imports of different versions of the POI libraries.


The docs say that the class implements Closeable. Thus it has a close() method and you can close the workbook like this:

XSSFWorkbook workbook = new XSSFWorkbook(fIP)

// Do your stuff;

workbook.close();

Since the class also implements AutoCloseable youn can go with a try-with-resources block as well:

try (XSSFWorkbook workbook = new XSSFWorkbook(fIP)) {
    // Do your stuff
}

If you use this approach the workbook will be closed automatically after the try block has finished.