Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix Guice error, "An illegal reflective access operation has occurred"

I've a Kotlin project which uses Guice for DI and has recently been updated from JDK 8 -> 11. It now emits the following error at runtime:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.google.inject.internal.cglib.core.$ReflectUtils$1 (file:/Users/matt/.gradle/caches/modules-2/files-2.1/com.google.inject/guice/4.2.2/6dacbe18e5eaa7f6c9c36db33b42e7985e94ce77/guice-4.2.2.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of com.google.inject.internal.cglib.core.$ReflectUtils$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

How should this warning be addressed?

like image 240
Matt R Avatar asked Sep 30 '19 10:09

Matt R


People also ask

How can you avoid warning an illegal reflective access operation has occurred?

Avoid illegal reflective accessUse a compatible and certified version of your software. If the software is actively maintained, then the developers are now aware of the warning and a fix may come in the near future.

What is com Google inject?

com.google.inject » guiceApache. Guice is a lightweight dependency injection framework for Java 8 and above, developed by Google. Last Release on Jan 25, 2022.


Video Answer


1 Answers

The issue is due to how Guice internally accesses Java objects, which is unsafe under the new (Java 9+) Java Module System.

Also, starting with Java 16, this warning becomes an error, and the execution flag --illegal-access=permit must be specified manually to reproduce the behaviour of Java 9-15.

There is not much you can do to fix it; the best option is to upgrade to Guice 5.0.1, which has been patched to avoid illegal accesses. Your warning will disappear, and your application will work on Java 16+ with the default JVM behaviour.

like image 185
Danilo Pianini Avatar answered Sep 18 '22 22:09

Danilo Pianini