2

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.

J Developer
  • 31
  • 1
  • 1
  • 4
  • IllegalAccessError is thrown when the programmer: `Accesses a private field, OR Modifies a final field OR Calls a private method` try with updated jar it may be possible jar have some issue – Subodh Joshi Jul 23 '15 at 07:35
  • Are you sure you don't have mis-matched POI jars? See [this POI FAQ for how to check](http://poi.apache.org/faq.html#faq-N10006) – Gagravarr Jul 24 '15 at 04:17
  • @ Gagravarr Hi Gagravarr, which would be the better jar to perform this : OPCPackage pkg = OPCPackage.open(filename); can you please suggest , Iam facing the the exception "Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/util/POILogFactory" when this line is getting executed. – J Developer Jul 27 '15 at 09:52
  • I go tthis error with POI 3.13. Reverted to 3.9, it works. Sticking with 3.9 for now. – Do Will Oct 14 '15 at 20:21

1 Answers1

4

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.

Giulio Caccin
  • 2,962
  • 6
  • 36
  • 57
Crane
  • 121
  • 2
  • 9