java17 报错 不添加 -add-opens 的方式 解决 Unable to make field final int javamathBigIntegersignum accessible module javabase does not opens javamath to unnamed module 79da8dc5
这个错误是由于在Java 17中的模块化系统引入了一些新的访问控制规则所导致的。具体来说,java.math.BigInteger类的signum字段被设置为final,在Java 17之前是可以访问的,但是在Java 17中默认是不可访问的。
要解决这个问题,可以使用--add-opens选项来打开java.math模块,允许未命名模块访问它。具体操作如下:
-
打开你的Java 17项目的构建配置文件(如Maven的
pom.xml或Gradle的build.gradle)。 -
在适当的位置添加以下行,以在构建过程中添加
--add-opens选项:对于Maven项目:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <compilerArgs> <arg>--add-opens</arg> <arg>java.base/java.math=ALL-UNNAMED</arg> </compilerArgs> </configuration> </plugin> </plugins> </build>对于Gradle项目:
tasks.withType(JavaCompile) { options.compilerArgs += '--add-opens' options.compilerArgs += 'java.base/java.math=ALL-UNNAMED' } -
保存并重新构建项目。
通过添加--add-opens选项,你可以打开java.math模块,允许未命名模块访问其中的字段和方法。这样,你应该能够解决这个报错
原文地址: https://www.cveoy.top/t/topic/iDfK 著作权归作者所有。请勿转载和采集!