直接使用JDK17导致EasyExcel无法使用的问题

背景

Oracle 官方宣布 JDK 17 可以免费商用,出于好奇决定用测试项目尝试一下。
之前一直在JDK1.8下进行开发,对于从JDK9开始启用的JPMS(Java Platform Module System)非常陌生,也想趁此机会多了解一些这方面的内容

问题

测试项目是一个springboot项目(maven项目),用于EasyExcel测试,在pom.xml中先调整JDK版本号

<properties>
	<maven.compiler.source>17</maven.compiler.source>
   	<maven.compiler.target>17</maven.compiler.target>
</properties>
<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
				<configuration>
				  <source>17</source>
				  <target>17</target>
				</configuration>
			  </plugin>
		</plugins>
	</build>

然后,尝试运行,控制台报错,内容如下:
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 @61832929
从告警信息看,存在模块引入异常,然而在创建了module-info.java以后,发现并不能解决这个问题(因为cglib)。

处理办法

我在easyexcle 的 github issues上找到的一个处理办法,是引入burningwave

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

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

StaticComponentContainer.Modules.exportAllToAll();

完整代码如下:

@SpringBootTest
class TestApplicationTests {

	@Test
	void contextLoads() {
        StaticComponentContainer.Modules.exportAllToAll();
		ExcelListener el = new ExcelListener();
        //String path 文件路径;
        File xfile = new File(path);
        if (xfile.exists() && xfile.isFile()) {
        	// User是一个实体类,用于映射表格的表头
            EasyExcel.read(xfile, User.class, el).sheet().doRead();
            List<User> all = el.getList();
            for (User user : all) {
                System.out.println(user.getName());
            }
        } else {
            System.out.println("文件不存在,即将新建");
            EasyExcel.write(xfile, User.class).sheet(0).doWrite(userData());
        }
	}
}

相关阅读

1.easyexcel github issue: Exception after upgrading JDK to version 16
2.导入所有模块:Exporting all modules to all modules at runtime on Java 16 and later
3.JMPS:JDK9的新特性:JPMS模块化

  • 21
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 19
    评论
评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值