70

My JDK 9+181 Spring Boot 2.0.0.BUILD-SNAPSHOT CLI application displays this warning on startup:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.springframework.cglib.core.ReflectUtils$1 (jar:file:/home/jan/src/fm-cli/target/fm-cli-0.1.0-SNAPSHOT.jar!/BOOT-INF/lib/spring-core-5.0.0.RELEASE.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 org.springframework.cglib.core.ReflectUtils$1

This is a console application, so I need to disable this warning -- how can I do that?

Note: This question asks the specific question of how to disable this warning triggered by Spring; it is not a duplicate of JDK9: An illegal reflective access operation has occurred. org.python.core.PySystemState which deals with a similar symptom in a different library.

Jan Nielsen
  • 10,892
  • 14
  • 65
  • 119

8 Answers8

92

In JDK 9+, add the following option to the JVM to disable the warning from Spring's use of CGLIB:

--add-opens java.base/java.lang=ALL-UNNAMED

for example:

java --add-opens java.base/java.lang=ALL-UNNAMED -jar target/*.jar

No need to report it; it's a known Spring bug.

This happens because the new JDK 9 module system detected an illegal access that will be disallowed sometime in the (near) future. You can read more about the JDK 9 Module system here.

Update:

A fix for this issue is available JDK 9+ with Spring 5.1+.

Jan Nielsen
  • 10,892
  • 14
  • 65
  • 119
  • 1
    I am using Spring Boot 2.0.0.RC1 with JDK 9.0.4, and face same problem. Can you explain more about the trick? What does the mean of ***switch***? – Vy Do Feb 15 '18 at 09:55
  • I also seen it at here https://github.com/dsyer/spring-boot-java-9 . I have difficult with set JVM options for IntelliJ IDEA. I don't know how to overcome. – Vy Do Feb 15 '18 at 16:13
  • 2
    @DoNhuVy -- consider asking a new question about how to do that. You can find more information here: https://intellij-support.jetbrains.com/hc/en-us/articles/206544869-Configuring-JVM-options-and-platform-properties – Jan Nielsen Feb 15 '18 at 16:15
  • 1
    I add question at here: https://stackoverflow.com/questions/48811990/set-jvm-option-for-avoid-error-illegal-reflective-access-by-org-springframework – Vy Do Feb 15 '18 at 16:31
  • 2
    Seems like the issue has appeared in JDK 10 as well- Can you explain how I can add `--add-opens java.base/java.lang=ALL-UNNAMED` to the JVM? I'm not familar how to edit that. – Airagale Mar 23 '18 at 18:52
  • If you're running from the command line, it's `java --add-open...` as noted above. If you're running in an environment, like an IDE, you'll have to identify the environment... – Jan Nielsen Mar 23 '18 at 20:22
  • 1
    The issue has since then been resolved and the fix will be part of Spring 5.1. – Pyves Apr 12 '18 at 18:02
  • should we configure the run configuration as vm argument? – subhashis Nov 09 '18 at 10:54
  • It works well after adding --add-opens java.base/java.lang=ALL-UNNAMED as VM option. Thanks, – Michael Qin Aug 09 '19 at 04:55
  • 1
    I am getting this warning with java 11 and spring-core-5.3.6 even though I have added --add-opens java.base/java.lang=ALL-UNNAMED. Any idea on this? – ric Jun 16 '21 at 14:34
  • @ric -- share a GitHub repository and I'll take a look – Jan Nielsen Jun 16 '21 at 14:39
  • @Jan, I cant share the github repository as its confidential – ric Jun 16 '21 at 14:50
  • Just a sample showing the problem, not the whole repository, @ric. Paring it down might help you find the problem as well... – Jan Nielsen Jun 16 '21 at 15:01
  • org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.lang=ALL-UNNAMED org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/sun.nio.ch=ALL-UNNAMED WARNING: Illegal reflective access by org.springframework.util.ReflectionUtils (file:/opt/tomcat/webapps/app/WEB-INF/lib/spring-core-5.3.6.jar) to constructor java.lang.invoke.MethodHandles$Lookup(java.lang.Class) WARNING: Please consider reporting this to the maintainers of org.springframework.util.ReflectionUtils – ric Jun 17 '21 at 05:31
  • I have added the log, where you can see I have added --add-opens java.base/java.lang=ALL-UNNAMED, but still getting issue. – ric Jun 17 '21 at 05:32
  • Same w/ springboot 2.3.1 spring framework 5.2.7, jdk11.0.12? Ideas? Avenues? – cp. Oct 27 '21 at 22:13
16

This also happened to me on JDK 11 and Spring Boot 2.2.1 (Spring Core 5.2.1).

What helped was removing the dependency to org.springframework.boot:spring-boot-devtools, as suggested in some comments to Spring Framework issue #22814.

Cos64
  • 1,617
  • 2
  • 19
  • 30
7

Adding to Jan Nielsen answer above, if you are using Intellij and Spring Boot 2.0.3, which depends on Spring core 5.0.7, you are still stuck and do not have the fix.

The way out for me needed two things:

  • Add the --add-opens mentioned by Jan to your run/debug configuration. Just edit the configuration and look under Environment / VM Options. This takes care of silencing some of the "illegal access messages".

  • I also needed to add a dependency to the jaxb-api library. I got the hint from ValentinBossi comment on this spring github issue.

luv2learn
  • 606
  • 8
  • 12
4

When using the spring initializer, make sure you use the latest version of Spring Boot. It automatically gets Spring core 5.1 or greater for you and you won't see the error when you run the project.

So you don't have to worry about editing any JVM configuration.

Onome Sotu
  • 666
  • 9
  • 18
4

After these warnings, if your app is still not working then add this dependency to your pom.xml

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.0</version>
</dependency>

This helped me!!

4

I'm sitting on these versions and I still see the WARNING:

  • SpringBoot 2.4.1
  • Spring Core FW: 5.3.2 (my parent pom is Spring Boot's, so this version is introduced by it, not manually set by me)
  • Java openjdk version "11.0.9.1" 2020-11-04

Removing the dependency from spring-boot-devtools also worked for me and the warning is no longer there at startup.

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-devtools</artifactId>
  <optional>true</optional>
</dependency>

... At the end of the day I was just using the automatic re-start from my IDE upon any change in the classpath and I can leave without it. The thing, though, is that theroreticatlly we should not worry about it, since according to Spring documentation:

Developer tools are automatically disabled when running a fully packaged application. If your application is launched using java -jar or if it’s started using a special classloader, then it is considered a “production application”

So one could expect that when the application is launched from command line the warning should go away (I haven't verified that).

WinterBoot
  • 399
  • 1
  • 5
  • 15
  • `So one could expect that when the application is launched from command line the warning should go away (I haven't verified that).` I can verify this one. But I really want to launch it from Intellij – Stan Aug 28 '22 at 23:17
0

In Spring Boot 3 and Java 19, I had similiar problem. My module configuration was incorrect in package-info.java file. My error was like below:

Caused by: java.lang.IllegalAccessError: class fetch.App$$EnhancerBySpringCGLIB$$4423c935 (in module ABC) cannot access class org.springframework.cglib.core.ReflectUtils (in unnamed module @0x50378a4) because module ABC does not read unnamed module @0x50378a4

In module ABC, please add in module-info.java:

    requires spring.context;
    requires spring.core;
    requires spring.beans;
sobczak.dev
  • 973
  • 7
  • 11
0

If you still have a problem or see an exception like this:

org.springframework.cglib.core.CodeGenerationException: java.lang.IllegalAccessException-->module spring.jpms does not open org.example to unnamed module @4cc77c2e
        at org.springframework.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:471) ~[spring-core-6.0.9.jar:6.0.9]...

Make sure you module-info.java requires correct set of spring modules and opens your module to them accordingly:

module spring.jpms {
  requires spring.core;
  requires spring.boot;
  requires spring.boot.autoconfigure;
  requires spring.context;
  requires spring.beans;

  opens org.example to
      spring.core,
      spring.beans,
      spring.context;
}

Make sure you include all the spring .jar files in java launcher's module-path.

Here is a complete example of a Spring Boot application that runs with Java Modules: https://github.com/agavrilov76/spring-jpms

Alexey Gavrilov
  • 10,593
  • 2
  • 38
  • 48