Adding to the answer by @kamiel-ahmadpour: I had to set the javaHome when running Gradle's compile tasks via IntelliJ. My config now looks like this written in Kotlin DSL:
tasks.withType<JavaCompile> {
    options.isFork = true
    options.forkOptions.executable = "javac"
    options.forkOptions.javaHome = file(System.getProperty("java.home"))
    options.compilerArgs.add("-XDignore.symbol.file")
}
Without the javaHome the build would fail with errors like:
... error: package javax.xml.bind.annotation does not exist
import javax.xml.bind.annotation.XmlAccessType;
                            ^
Running Gradle tasks from the terminal doesn't need the explicitly configured javaHome and I didn't find the actual cause for the different behaviour, yet.
Environment:
- Java 8
 
- Gradle 7.5.1
 
- IntelliJ 2022.2.3