环境

JDK:21
Spring Boot:3.4.2
EasyExcel:3.0.3 (EasyExcel 3.1.0+版本不存在该问题)

报错

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 @22f71333

接口

@PostMapping("/import/users")
public List<UserExcelData> importUsers(@RequestParam("file") MultipartFile file) {
    try {
        UserExcelDataListener listener = new UserExcelDataListener();
        EasyExcel.read(file.getInputStream(), UserExcelData.class, listener).sheet().doRead();
        List<UserExcelData> users = listener.getUsers();
        return users;
    } catch (IOException e) {
        e.printStackTrace();
        return null; 
    }
}

解决方法

1、引入依赖

<dependency>
    <groupId>org.burningwave</groupId>
    <artifactId>core</artifactId>
    <version>9.5.2</version>
</dependency>

2、在接口中添加一行代码,用于引入所有模块

StaticComponentContainer.Modules.exportAllToAll();

完整代码

@PostMapping("/import/users")
public List<UserExcelData> importUsers(@RequestParam("file") MultipartFile file) {
    try {
		StaticComponentContainer.Modules.exportAllToAll(); // 添加这行
        UserExcelDataListener listener = new UserExcelDataListener();
        EasyExcel.read(file.getInputStream(), UserExcelData.class, listener).sheet().doRead();
        List<UserExcelData> users = listener.getUsers();
        return users;
    } catch (IOException e) {
        e.printStackTrace();
        return null; 
    }
}

参考:Exception after upgrading JDK to version 16

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐