org.springframework.cglib.core.ReflectUtils$1的非法反射访问

71

我的JDK 9+181 Spring Boot 2.0.0.BUILD-SNAPSHOT CLI应用程序在启动时显示以下警告:

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
这是一个控制台应用程序,我需要禁用这个警告 - 我该怎么做? 注意:此问题询问如何禁用Spring触发的此警告;它不是JDK9:An illegal reflective access operation has occurred. org.python.core.PySystemState的重复问题,后者涉及不同库中的类似症状。
8个回答

92
在JDK 9+中,将以下选项添加到JVM中以禁用Spring对CGLIB的使用所产生的警告:
--add-opens java.base/java.lang=ALL-UNNAMED

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

不需要报告它; 这是一个已知的 Spring bug

这种情况发生是因为新的JDK 9模块系统检测到一个非法访问,这将在(不久的)未来被禁止。您可以在此处了解有关JDK 9模块系统的更多信息

更新:

使用Spring 5.1+的JDK 9+可用修复此问题。


1
我正在使用Spring Boot 2.0.0.RC1和JDK 9.0.4,并遇到了同样的问题。你能更详细地解释一下这个技巧吗?switch是什么意思? - Raphaël Colantonio
2
@DoNhuVy -- 考虑提出一个新问题,询问如何做到这一点。您可以在此处找到更多信息:https://intellij-support.jetbrains.com/hc/en-us/articles/206544869-Configuring-JVM-options-and-platform-properties - Jan Nielsen
1
我在这里添加一个问题:https://dev59.com/Danka4cB1Zd3GeqPIhcG - Raphaël Colantonio
2
似乎问题也出现在JDK 10中-您能解释一下如何将 --add-opens java.base/java.lang=ALL-UNNAMED 添加到JVM中吗?我不熟悉如何进行编辑。 - Airagale
1
我在使用Java 11和Spring-core-5.3.6时遇到了这个警告,尽管我已经添加了--add-opens java.base/java.lang=ALL-UNNAMED。对此有什么想法吗? - ric
显示剩余11条评论

16

这在JDK 11和Spring Boot 2.2.1(Spring Core 5.2.1)中也发生过。

有帮助的是删除对org.springframework.boot:spring-boot-devtools的依赖,正如一些 评论所建议的那样,参见Spring Framework问题#22814


7

除了Jan Nielsen的回答,如果您正在使用Intellij和Spring Boot 2.0.3,它依赖于Spring core 5.0.7,您仍然会遇到问题。

对我来说,解决方法需要两个步骤:

  • 在运行/调试配置中添加Jan提到的--add-opens。只需编辑配置并查看Environment / VM Options。这将消除一些“非法访问消息”。

  • 我还需要向jaxb-api库添加依赖项。我从ValentinBossi在this spring github issue上的评论中得到了提示。


4
当使用Spring Initializer时,请确保您使用最新版本的Spring Boot。它会自动为您获取Spring Core 5.1或更高版本,这样当您运行项目时就不会看到错误信息。
因此,您无需担心编辑任何JVM配置。

1
我在5.3.3上遇到了这个问题。Spring Boot版本为2.4.2。 - The Coder

4
在收到这些警告后,如果您的应用仍然无法正常工作,则需要将以下依赖项添加到您的pom.xml文件中。
<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.0</version>
</dependency>

这对我很有帮助!

4
我还在使用以下版本,但是看到了警告消息:
  • SpringBoot 2.4.1
  • Spring Core FW: 5.3.2(我的父POM是Spring Boot的,所以这个版本是由它引入的,并非我手动设置的)
  • Java openjdk version "11.0.9.1" 2020-11-04
从spring-boot-devtools中删除依赖项也适用于我,启动时不再显示警告。
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-devtools</artifactId>
  <optional>true</optional>
</dependency>

最终,我只是利用了IDE中的自动重新启动功能,一旦类路径有任何更改就能够离开,但问题在于理论上我们不应该担心这个问题,因为根据Spring文档

运行完全封装的应用程序时,开发者工具会自动停用。如果您的应用程序使用java -jar启动或使用特殊的类加载器启动,则被视为“生产应用程序”

因此,可以预期当应用程序从命令行启动时,警告消息应该会消失(我没有验证过)。


所以可以期望当应用程序从命令行启动时,警告应该消失(我还没有验证过)。但我真的想从Intellij启动它。 - Stan

0
在Spring Boot 3和Java 19中,我遇到了类似的问题。我的模块配置在package-info.java文件中不正确。我的错误信息如下:
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

请在模块ABC中的module-info.java文件中添加以下内容:
    requires spring.context;
    requires spring.core;
    requires spring.beans;

0

如果您仍然遇到问题或看到类似这样的异常:

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]...

请确保你的 module-info.java 文件正确地声明了所需的 Spring 模块,并对它们进行了适当的开放。
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;
}

请确保在Java启动器的模块路径中包含所有Spring .jar文件。

这是一个完整的Spring Boot应用程序示例,可与Java模块一起运行: https://github.com/agavrilov76/spring-jpms


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接