报错:

①org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘globalTransactionScanner’ defined in class path resource [io/seata/spring/boot/autoconfigure/SeataAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.seata.spring.annotation.GlobalTransactionScanner]: Factory method ‘globalTransactionScanner’ threw exception; nested exception is java.lang.ExceptionInInitializerError
②Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not “opens java.lang” to unnamed module @69453e37

解决方法:

这个错误通常是由于 Java 9 及以上版本引入的模块系统(JPMS)导致的,特别是在使用反射时访问某些受保护的类或方法。
要解决这个问题,可以尝试以下几种方法:

添加 JVM 启动参数:
在启动你的 Spring Boot 应用时,添加以下 JVM 参数来开放模块:

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

具体操作方式因运行方式不同而异:

  • 在 IDE 中(如 IntelliJ IDEA 或 Eclipse),你可以在运行配置中找到“VM options”或“Program arguments”选项,添加上述参数。
  • 在命令行中,可以这样运行:
java --add-opens java.base/java.lang=ALL-UNNAMED -jar your-app.jar

具体添加方法步骤:

你可以在不同的环境中以不同的方式添加 --add-opens 参数。下面是几种常见的方法:

1. 在 IDE 中添加

IntelliJ IDEA
  1. 打开项目,点击顶部菜单的 Run
  2. 选择 Edit Configurations
  3. 在运行配置中,找到 VM options 输入框。
  4. 添加 --add-opens java.base/java.lang=ALL-UNNAMED,然后点击 OK
Eclipse
  1. 右击你的项目,选择 Run As > Run Configurations
  2. 选择你的 Java 应用配置。
  3. Arguments 选项卡中,找到 VM arguments 输入框。
  4. 添加 --add-opens java.base/java.lang=ALL-UNNAMED,然后点击 ApplyRun

2. 在命令行中添加

如果你是通过命令行运行 JAR 文件,可以在命令中直接添加参数。例如:

java --add-opens java.base/java.lang=ALL-UNNAMED -jar your-app.jar

3. 在构建工具中添加

如果你使用 Maven 或 Gradle,可以在构建配置中添加这个参数。

Maven

pom.xml 中,添加以下插件配置:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <fork>true</fork>
                <compilerArgs>
                    <arg>--add-opens</arg>
                    <arg>java.base/java.lang=ALL-UNNAMED</arg>
                </compilerArgs>
            </configuration>
        </plugin>
    </plugins>
</build>
Gradle

build.gradle 文件中,添加以下配置:

tasks.withType(JavaExec) {
    jvmArgs '--add-opens', 'java.base/java.lang=ALL-UNNAMED'
}

按照你的项目环境选择合适的方法,添加完参数后重新运行你的应用。

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐