Java – Exception in thread “main” java.lang.IllegalAccessError:

apache-poijava

I am getting the below Exception when I am trying to read an Excel file:

Exception in thread "main" java.lang.IllegalAccessError: tried to access method org.apache.poi.util.POILogger.log(ILjava/lang/Object;)V from class org.apache.poi.openxml4j.opc.PackageRelationshipCollection
    at org.apache.poi.openxml4j.opc.PackageRelationshipCollection.parseRelationshipsPart(PackageRelationshipCollection.java:304)
    at org.apache.poi.openxml4j.opc.PackageRelationshipCollection.<init>(PackageRelationshipCollection.java:156)
    at org.apache.poi.openxml4j.opc.PackageRelationshipCollection.<init>(PackageRelationshipCollection.java:124)
    at org.apache.poi.openxml4j.opc.PackagePart.loadRelationships(PackagePart.java:559)
    at org.apache.poi.openxml4j.opc.PackagePart.<init>(PackagePart.java:112)
    at org.apache.poi.openxml4j.opc.PackagePart.<init>(PackagePart.java:83)
    at org.apache.poi.openxml4j.opc.PackagePart.<init>(PackagePart.java:128)
    at org.apache.poi.openxml4j.opc.ZipPackagePart.<init>(ZipPackagePart.java:78)
    at org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:218)
    at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:662)
    at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:223)
    at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:186)
    at readAndWriteToExcel.ReadingExcel.main(ReadingExcel.java:36)

My code is:

package readAndWriteToExcel;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;

public class ReadingExcel {
        private static final Log LOG = LogFactory
                .getLog(ReadingExcel.class);
        public static void main(String[] args) throws Exception {
            String SAMPLE_PERSON_DATA_FILE_PATH = "C:/Users/Documents/Test Data 5.xlsx";
            File file = new File(SAMPLE_PERSON_DATA_FILE_PATH);
            InputStream inputStream = new FileInputStream(file);

            // The package open is instantaneous, as it should be.
            OPCPackage pkg = null;
            try {
                ExcelWorkSheetRowCallbackHandler sheetRowCallbackHandler = new ExcelWorkSheetRowCallbackHandler(
                        new ExcelRowContentCallback() {
                            public void processRow(int rowNum,    Map<String, String> map) {
                                // Do any custom row processing here, such as save
                                // to database
                                // Convert map values, as necessary, to dates or
                                // parse as currency, etc
                                System.out.println("rowNum=" + rowNum + ", map=" + map);
                            }
                        });

                pkg = OPCPackage.open(inputStream);
                ExcelSheetCallBack sheetCallback = new ExcelSheetCallBack() {
                    private int sheetNumber = 0;


                    public void startSheet(int sheetNum) {
                        this.sheetNumber = sheetNum;
                        System.out.println("Started processing sheet number=" + sheetNumber);
                    }


                    public void endSheet() {
                        System.out.println("Processing completed for sheet number=" + sheetNumber);
                    }
                };

                System.out.println("Constructor: pkg, sheetRowCallbackHandler, sheetCallback");
                ExcelReader example1 = new ExcelReader(pkg,
                        sheetRowCallbackHandler, sheetCallback);
                example1.process();

                System.out.println("nConstructor: filePath, sheetRowCallbackHandler, sheetCallback");

                ExcelReader example2 = new ExcelReader(SAMPLE_PERSON_DATA_FILE_PATH, sheetRowCallbackHandler, sheetCallback);
                example2.process();

                System.out.println("nConstructor: file, sheetRowCallbackHandler, sheetCallback");

                ExcelReader example3 = new ExcelReader(file,
                        sheetRowCallbackHandler, null);
                example3.process();
            } catch (RuntimeException are) {
                LOG.error(are.getMessage(), are.getCause());
            } catch (InvalidFormatException ife) {
                LOG.error(ife.getMessage(), ife.getCause());
            } catch (IOException ioe) {
                LOG.error(ioe.getMessage(), ioe.getCause());
            } finally {
                IOUtils.closeQuietly(inputStream);
                try {
                    if (null != pkg) {
                        pkg.close();
                    }
                } catch (IOException e) {
                    // just ignore IO exception
                }
            }
        }
    }

Is there a simple way to read and edit an Excel file (xls and xlsx) with more than 50k records? I have searched a lot and worked with a few available codes.
But I was not successful, I keep ending up with one Exception or another.

Best Answer

I was getting the same error:

"Handler processing failed; nested exception is java.lang.IllegalAccessError: tried to access method org.apache.poi.util.POILogger.log(ILjava/lang/Object;)V from class org.apache.poi.openxml4j.opc.PackageRelationshipCollection"

After updating maven and trying to access PackageRelationshipCollection and POILogger classes in eclipse things worked fine for me. Make sure you have all the required jars i.e poi, poi-ooxml, poi-ooxml-schemas.