13

After changing the Java version to 17 I can't build the Gradle project.

I am using Gradle 7.3.1 version and have the following line in Gradle properties:

org.gradle.jvmargs=-Dfile.encoding=UTF-8 -Xms1g -Xmx4g -XX:+UseG1GC -XX:+CMSClassUnloadingEnabled

then I got the following error

Unrecognized VM option 'CMSClassUnloadingEnabled'
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

If I remove -XX:+CMSClassUnloadingEnabled then I got this error:

Unable to make field private int java.lang.reflect.Field.modifiers accessible: module java.base does not "opens java.lang.reflect" to unnamed module @1b9ee3e0
kriegaex
  • 63,017
  • 15
  • 111
  • 202
Wiktor Kęska
  • 411
  • 2
  • 4
  • 13
  • 10
    The "Unrecognized VM option 'CMSClassUnloadingEnabled'" arises because in Java 14 the CMS garbage collector was removed and the options specific to that garbage collector (like "CMSClassUnloadingEnabled") are illegal for Java 14 and later. If you add this option the JVM refuses to start. – Thomas Kläger Dec 03 '21 at 14:14
  • 4
    The second error "Unable to make field private int java.lang.reflect.Field.modifiers accessible" happens because Java 17 drastically restricted access to internal fields. However, since Gradle 7.3 is compatible with Java 17 it must be an error reported from some third party plugin or from your code. Without further context this cannot be decided. – Thomas Kläger Dec 03 '21 at 14:16
  • Yeah you are right that's because of AspectJ – Wiktor Kęska Dec 04 '21 at 12:58
  • 5
    A typical case of esoteric command line options. The option `-XX:+CMSClassUnloadingEnabled` is, as the name indicates, specific to the CMS garbage collector and hence, never made any sense in combination with `-XX:+UseG1GC`, an entirely different garbage collector. So, now that the CMS collector has been removed, options related to CMS cause an error, but these options had no effect anyway when you were using G1GC. Since G1GC is the default in JDK 17, you can even remove the `-XX:+UseG1GC` option. – Holger Dec 06 '21 at 11:46

1 Answers1

0
Unable to make field private int java.lang.reflect.Field.modifiers accessible:
module java.base does not "opens java.lang.reflect" to unnamed module @1b9ee3e0

Yeah you are right that's because of AspectJ

Assuming that you are using AspectJ load-time weaving (LTW) rather than compile-time weaving, maybe you ought to read the AspectJ release notes for each version since 1.9.7, supporting Java 16+:

Use LTW on Java 16+

Please note that if you want to use load-time weaving on Java 16+, the weaving agent collides with JEP 396 (Strongly Encapsulate JDK Internals by Default) and related subsequent JEPs. Therefore, you need to set the JVM parameter --add-opens java.base/java.lang=ALL-UNNAMED in order to enable aspect weaving. This is due to the fact that the weaver uses internal APIs for which we have not found an adequate replacement yet when defining classes in different classloaders.

kriegaex
  • 63,017
  • 15
  • 111
  • 202