10

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.

user2304483
  • 1,462
  • 6
  • 28
  • 50
  • hmmm this might be helpful or at least worth a read -> https://stackoverflow.com/questions/41265266/how-to-solve-inaccessibleobjectexception-unable-to-make-member-accessible-m – SpaceCowboy Jul 02 '21 at 08:17
  • For now, you can probably work around the error with `--illegal-access=permit`. But whatever code that is trying to access that method should be fixed (see https://stackoverflow.com/questions/50471466/what-to-use-instead-of-defineclass-in-java10) – Slaw Jul 02 '21 at 08:39
  • I prefer to fix it cause I'm starting new project, do you know what I should change in the code, I have simple pojo – user2304483 Jul 02 '21 at 09:00
  • Are you using any libraries/frameworks? – Slaw Jul 02 '21 at 09:48
  • Of course, springboot – user2304483 Jul 02 '21 at 12:28
  • Then it is the responsibility of Spring to fix the problem. Frameworks like that often define classes at run-time and ideally would have switched from `ClassLoader#defineClass(...)` to `Lookup#defineClass(....)` by now. Are you using the latest version of Spring Boot? – Slaw Jul 02 '21 at 23:56
  • the parent in the pom file is 2.5.2 – user2304483 Jul 03 '21 at 06:51

2 Answers2

8

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; i.e. 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 leads to warning messages since Java 9 and is blocked in Java 14 and later. This Q&A gives some background:

The simple and best solution is to use only "supported" versions of Java for your selected versions of Spring and Spring Boot.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
-2

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