简体   繁体   中英

Execution failed for task ':app:compileJava' with ./gradlew run

When I try using "./gradlew run" on my gradle projects, I receive this error:

* What went wrong:
Execution failed for task ':app:compileJava'.
> java.lang.IllegalAccessError: class org.gradle.internal.compiler.java.ClassNameCollector (in unnamed module @0x1fa4cfc5) cannot access class com.sun.tools.javac.code.Symbol$TypeSymbol (in module jdk.compiler) because module jdk.compiler does not export com.sun.tools.javac.code to unnamed module @0x1fa4cfc5

I have tried on multiple different projects and it still doesn't work. I believe it has something to do with gradle expecting a different version of Java, as I am using java 16 and when I check "gradle -v" it says "JVM: 15.0.2". I am not sure how to fix this or if this is even the problem. Any help would be greatly appreciated!

Turns out I had multiple paths for java in my Path system variable. I deleted all but the correct one and then added a JAVA_HOME system variable with a value of the correct path for JDK (C:\Program Files\Java\jdk-"version").

Using JDK 17 I was also receiving an error message like

java.lang.IllegalAccessError: class org.gradle.internal.compiler.java.ClassNameCollector (in unnamed module @0x1fa4cfc5) cannot access class com.sun.tools.javac.code.Symbol$TypeSymbol (in module jdk.compiler) because module jdk.compiler does not export com.sun.tools.javac.code to unnamed module @0x1fa4cfc5

The only solution that I could find was to create an environment variable

JDK_JAVA_OPTIONS='--add-exports jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED'

I think the root cause of this issue is a mismatch between the Gradle version and the JDK version in use. One should refer to the compatibility matrix .

For example, for a Java 17 runtime, one needs at least Gradle 7.3.

I hit the "IllegalAccessError" issue and in my case, it was solved by selecting "Specific Gradle version" 7.4 in Eclipse Gradle (Buildship) preferences.

Ensuring the correct match of JDK and Gradle version is environment-specific but I think once this is ascertained, the issue goes away.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM