邵泽龙 2021-09-19 22:10 采纳率: 0%
浏览 1405

EasyExcel写入xlsx文件失败:java.lang.ExceptionInInitializerError

今天在使用EasyExcel按照官方代码测试写入xlsx文件时,提示以下错误,尝试各种方案无效,咨询网友给与指点

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" com.alibaba.excel.exception.ExcelGenerateException: java.lang.ExceptionInInitializerError
    at com.alibaba.excel.write.ExcelBuilderImpl.addContent(ExcelBuilderImpl.java:64)
    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:61)
    at application.excel.easy.EasyTest.simpleWrite(EasyTest.java:41)
    at application.excel.easy.EasyTest.main(EasyTest.java:16)
Caused by: java.lang.ExceptionInInitializerError
    at net.sf.cglib.core.KeyFactory$Generator.generateClass(KeyFactory.java:166)
    at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
    at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
    at net.sf.cglib.core.KeyFactory$Generator.create(KeyFactory.java:144)
    at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:116)
    at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:108)
    at net.sf.cglib.beans.BeanMap$Generator.<clinit>(BeanMap.java:64)
    at net.sf.cglib.beans.BeanMap.create(BeanMap.java:56)
    at com.alibaba.excel.write.executor.ExcelWriteAddExecutor.addJavaObjectToExcel(ExcelWriteAddExecutor.java:119)
    at com.alibaba.excel.write.executor.ExcelWriteAddExecutor.addOneRowOfDataToExcel(ExcelWriteAddExecutor.java:72)
    at com.alibaba.excel.write.executor.ExcelWriteAddExecutor.add(ExcelWriteAddExecutor.java:56)
    at com.alibaba.excel.write.ExcelBuilderImpl.addContent(ExcelBuilderImpl.java:58)
    ... 5 more
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @1996cd68
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:357)
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
    at java.base/java.lang.reflect.Method.checkCanSetAccessible(Method.java:199)
    at java.base/java.lang.reflect.Method.setAccessible(Method.java:193)
    at net.sf.cglib.core.ReflectUtils$2.run(ReflectUtils.java:56)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:312)
    at net.sf.cglib.core.ReflectUtils.<clinit>(ReflectUtils.java:46)
    ... 17 more

代码完全按照官方提供的内容写的两个类如下:

EasyTest.java

package application.excel.easy;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class EasyTest {

    static String  PATH = "D:\\MyWorld\\Gitee\\my-java\\example\\src\\main\\resources\\";

    public static void main(String[] args) {
        EasyTest easyTest = new EasyTest();
        easyTest.simpleWrite();
    }

    private List<DamoData> data() {
        List<DamoData> list = new ArrayList<DamoData>();
        for (int i = 0; i < 10; i++) {
            DamoData data = new DamoData();
            data.setString("字符串" + i);
            data.setDate(new Date());
            data.setDoubleData(0.56);
            list.add(data);
        }
        return list;
    }

    /**
     * 最简单的写
     * <p>1. 创建excel对应的实体对象 参照{@link DamoData}
     * <p>2. 直接写即可
     */
    public void simpleWrite() {
        // 写法1
        String fileName = PATH + "easyExcel.xlsx";
        // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
        // 如果这里想使用03 则 传入excelType参数即可
        EasyExcel.write(fileName, DamoData.class).excelType(ExcelTypeEnum.XLSX).sheet("模板").doWrite(data());
    }

}

DamoData.java

package application.excel.easy;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;

import java.util.Date;


@Data
public class DamoData {
    @ExcelProperty(value = "字符串标题")
    private String string;
    @ExcelProperty(value = "日期标题")
    private Date date;
    @ExcelProperty(value = "数字标题")
    private Double doubleData;
    /**
     * 忽略这个字段
     * */
    @ExcelIgnore
    private String ignore;
}

依赖导入

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>my_example</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>16</maven.compiler.source>
        <maven.compiler.target>16</maven.compiler.target>
    </properties>

    <!--导入依赖-->
    <dependencies>
        <!--日期格式化工具-->
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.10.1</version>
        </dependency>
        <!--测试工具-->
        <!--easyExcel:注意使用时检查依赖,避免重复依赖引起冲突-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.2.10</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

</project>

img

已经发现问题所在范围(我补充回答在下面),根本性解决思路还有待完善

  • 写回答

2条回答 默认 最新

  • 邵泽龙 2021-09-22 14:32
    关注

    最后测试发现,是由于JDK版本导致easyExcel模块无法正常运行,出现提示问题的JDK版本是16,我更换为更早期的JDK8,就可以正常使用了;怀疑错误提示:

    Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @1996cd68

    指出和反射调用JDK有关系,高版本的JDK可以对此有安全性其他改变,导致错误出现,在JDK16下使用easyExcel模块的办法还在继续研究中

    评论

报告相同问题?

问题事件

  • 修改了问题 9月22日
  • 创建了问题 9月19日

悬赏问题

  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分合并
  • ¥20 pcf8563时钟芯片不启振