线程“main”java.lang.IllegalAccessError 中的异常:

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31580826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:49:18  来源:igfitidea点击:

Exception in thread "main" java.lang.IllegalAccessError:

javaapache-poi

提问by J Developer

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

当我尝试读取 Excel 文件时,出现以下异常:

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.

是否有一种简单的方法可以读取和编辑超过 50k 条记录的 Excel 文件(xls 和 xlsx)?我已经搜索了很多并使用了一些可用的代码。
但是我没有成功,我一直以一个或另一个异常结束。

回答by Crane

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"

“处理程序处理失败;嵌套异常是 java.lang.IllegalAccessError:试图从类 org.apache.poi.openxml4j.opc 访问方法 org.apache.poi.util.POILogger.log(ILjava/lang/Object;)V。包关系集合”

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

更新Maven和试图访问后PackageRelationshipCollectionPOILogger班日食事情的来龙去脉对我很好。确保您拥有所有必需的 jar,即 poi、poi-ooxml、poi-ooxml-schemas。