[alibaba/easyexcel]读文件和写文件都抛java.lang.ExceptionInInitializerError

2023-12-08 982 views
3

触发场景描述 引入依赖,按照demo示例读文件和写文件

触发Bug的代码

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.1.4</version>
        </dependency>
@Data
public class MyLogEntity {
    @ExcelProperty("dt")
    private String dt;
    @ExcelProperty("hour")
    private String hour;
    @ExcelProperty("ctime")
    private String ctime;
    @ExcelProperty("ip")
    private String ip;
    @ExcelProperty("status")
    private String status;

    public String getDt() {
        return dt;
    }

    public void setDt(String dt) {
        this.dt = dt;
    }

    public String getHour() {
        return hour;
    }

    public void setHour(String hour) {
        this.hour = hour;
    }

    public String getCtime() {
        return ctime;
    }

    public void setCtime(String ctime) {
        this.ctime = ctime;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}
public class MyLogEntityListener extends AnalysisEventListener<MyLogEntity> {
    private static final Logger LOGGER = LoggerFactory.getLogger(MyLogEntityListener.class);
    private static final Gson GSON = new Gson();

    @Override
    public void invoke(MyLogEntity myLogEntity, AnalysisContext analysisContext) {
        LOGGER.info("解析到一条数据:{}", GSON.toJson(myLogEntity));
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        LOGGER.info("所有数据解析完成!");
    }
}
public class ExcelTest {
    @Test
    public void read() {
        String fileName = "/Users/turui/Desktop/excel-test.xlsx";
        EasyExcel.read(fileName, MyLogEntity.class, new MyLogEntityListener()).sheet().doRead();
    }

    @Test
    public void write() {
        List<MyLogEntity> data = Lists.newArrayList();
        for (int i = 0;i < 10; i++) {
            MyLogEntity myLogEntity = new MyLogEntity();
            myLogEntity.setDt("20190101");
            data.add(myLogEntity);
        }
        String fileName = "/Users/turui/Desktop/test_write" + System.currentTimeMillis() + ".xlsx";
        EasyExcel.write(fileName, MyLogEntity.class).sheet("模板1").doWrite(data);
    }
}

提示的异常或者没有达到的效果 读写文件都会抛出异常

com.alibaba.excel.exception.ExcelGenerateException: java.lang.ExceptionInInitializerError at com.alibaba.excel.write.ExcelBuilderImpl.addContent(ExcelBuilderImpl.java:67) at com.alibaba.excel.ExcelWriter.write(ExcelWriter.java:161) at com.alibaba.excel.ExcelWriter.write(ExcelWriter.java:146) at com.alibaba.excel.write.builder.ExcelWriterSheetBuilder.doWrite(ExcelWriterSheetBuilder.java:190) at com.meituan.rc.holmes.service.util.ExcelTest.write(ExcelTest.java:29) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) Caused by: java.lang.ExceptionInInitializerError at net.sf.cglib.beans.BeanMap.create(BeanMap.java:57) at com.alibaba.excel.write.executor.ExcelWriteAddExecutor.addJavaObjectToExcel(ExcelWriteAddExecutor.java:114) at com.alibaba.excel.write.executor.ExcelWriteAddExecutor.addOneRowOfDataToExcel(ExcelWriteAddExecutor.java:65) at com.alibaba.excel.write.executor.ExcelWriteAddExecutor.add(ExcelWriteAddExecutor.java:51) at com.alibaba.excel.write.ExcelBuilderImpl.addContent(ExcelBuilderImpl.java:61) ... 26 more Caused by: java.lang.IllegalStateException: Unable to load cache item at net.sf.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:79) at net.sf.cglib.core.internal.LoadingCache.get(LoadingCache.java:34) at net.sf.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:116) at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:291) at net.sf.cglib.core.KeyFactory$Generator.create(KeyFactory.java:221) at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:174) at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:157) at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:149) at net.sf.cglib.beans.BeanMap$Generator.(BeanMap.java:66) ... 31 more Caused by: java.lang.IncompatibleClassChangeError: class net.sf.cglib.core.DebuggingClassWriter has interface org.objectweb.asm.ClassVisitor as super class at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:763) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) at java.net.URLClassLoader.defineClass(URLClassLoader.java:467) at java.net.URLClassLoader.access$100(URLClassLoader.java:73) at java.net.URLClassLoader$1.run(URLClassLoader.java:368) at java.net.URLClassLoader$1.run(URLClassLoader.java:362) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:361) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at net.sf.cglib.core.DefaultGeneratorStrategy.getClassVisitor(DefaultGeneratorStrategy.java:30) at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:24) at net.sf.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:329) at net.sf.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:93) at net.sf.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:91) at net.sf.cglib.core.internal.LoadingCache$2.call(LoadingCache.java:54) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at net.sf.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:61) ... 39 more

回答

0

原因已经找到了,我们项目依赖的spring-boot是2.0+的版本,里面使用的asm版本7.0,而easyexcel里面依赖的asm版本是4.2,升级到7.0后会出问题,这个错是cglib抛出的,没有办法兼容。我只能放弃使用easyexcel了,希望能早点升级到2.0+的spring-boot版本

7

感谢您提供的思路,我这边的hadoop-commons里面的asm包和easyexcel的包冲突

2

仓库1.1.1版本没有asm 可以尝试使用1.1.1版本,我也有同样的问题 asm与asm-all冲突 导致javax.servlet.ServletException: org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.IncompatibleClassChangeError: Found class org.objectweb.asm.MethodVisitor, but interface was expected

6

难搞 容器中asm版本都不兼容,又改不了;

8

我看这个issue被关了,但是问题有修复么?我用最新版2.2.6一样有这个问题。

0

太坑了,docker容器报这个错。害我调试打包几十次,坑死人了。,也不知道这个问题解决了吗,用的最新2.2.6的

9

spring boot 2.0+,把cglib改成3.1,可以正常使用,cglib 3.3会抛 com.alibaba.excel.exception.ExcelAnalysisException: java.lang.ExceptionInInitializerError

用这个方法问题解决了,直接依赖 cglib 3.1 版本

9
    <dependency>
        <groupId>org.ow2.asm</groupId>
        <artifactId>asm</artifactId>
        <version>9.2</version>
    </dependency>

引用asm后正常使用