简体   繁体   中英

How to fix "Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass?

I've started a project using java 16 and I'm getting errors in the project all over the place:

An error has occurred. See error log for more details.
Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @6dfcffb5

It looks like the code I have from different version of java is not working in this particular version. Any idea what I need to change maybe in the pojo class. I have only 1 pojo in the application cause it is micro service.

Thanks.

You are using old versions of Spring / Spring Boot that are incompatible with, and do not support Java 16.

According to the Spring Compatibility Matrix , you will need to use Spring 5.3.x or later for Java 16 compatibility.

For Spring-Boot, you need 2.7.1 or later for Java 17 compatibility... according to this Spring And Spring Boot Versions page.

Also note that Java 16 is end-of-life as well. You probably should be using one of the LTS versions of Java; ie Java 8, 11 or 17.

(Winding back to an earlier Java LTS would "fix" the Spring compatibility issue, but IMO rolling Spring and Java forward is better. You don't want to be developing a new project on top of base that is already outdated.)


The actual problem you are seeing is due to something (probably a Spring Boot custom classloader) accessing an "internal" API. This is blocked (in Java 14 and later) unless you do some tweaking to the way that the JVM is launched. This Q&A gives some background:

However, the best solution is to use "supported" versions of Java for your selected versions of Spring and Spring Boot.

You can just define the JRE system library by precising the path following the next steps: Project Folder -> Properties -> Java build path -> double clicks on JRE system library and choose your installed JRE, it works for me. And sorry for my bad English

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM