I faced the same error with different JDK version, so i used the following code which works fine.
compileJava {
options.fork = true
options.forkOptions.javaHome = file('/Library/Java/JavaVirtualMachines/jdk-11.0.5.jdk/Contents/Home')
targetCompatibility = 11
sourceCompatibility = 11
}