Because you try to use Groovy DSL code in a Kotlin DSL file.
Those are different languages.
In Kotlin DSL it would be tasks.withType<JavaCompile> { ... } or tasks.withType(JavaCompile::class) { ... } or tasks.withType(JavaCompile::class.java) { ... }, all three would work.
But you should use neither of them, as you break task configuration avoidance for all JavaCompile tasks that way.
Instead you should use tasks.withType<JavaCompile>().configureEach { ... } or tasks.withType(JavaCompile::class).configureEach { ... } or tasks.withType(JavaCompile::class.java).configureEach { ... }.
Of course the following line will also not work, as again this is Groovy DSL code that you cannot use like that in a Kotlin DSL file.