Can't import java package on custom plugin

I’ve created Java or Kotlin Library on Android Studio.
I selected Kotlin and Kotlin DSL.

here is my build.gradle.kts
build.gradle.kts

plugins {
    java
    `kotlin-dsl`
    `java-library`
    id("java")
}

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(21)
    }
}

sourceSets {
    main {
        java.srcDir("src/main/java")
        kotlin.srcDir("src/main/kotlin")
    }
}

Then to create custom plugin make a Kotlin file, but cannot resolve java.util.Optional

import org.gradle.jvm.toolchain.JavaToolchainRequest
import org.gradle.jvm.toolchain.JavaToolchainResolver
import java.util.Optional

abstract class ResolverTest: JavaToolchainResolver {
    override fun resolve(request: JavaToolchainRequest): Optional {
        TODO("Not yet implemented")
    }
}

What else I’ve missing?
How can I resolve java.util.Optional?
Any suggestions?
Thank you.

Do you get the same error if you try to build through Gradle?

I suspect that simply your JDK in the IDE is not set properly.

Btw. the whole “sourceSets” block is useless, you just configure again what already is the default.

Thank you for your answer.
I hardly understand Do you get the same error if you try to build through Gradle?
That means I would run the build task?

I suspect that simply your JDK in the IDE is not set properly.

How can I set property it? Does toolchain doesn’t help it?

Btw. the whole “sourceSets” block is useless, you just configure again what already is the default.

I will remove that sourceSets, thank you.

That means I would run the build task?

For example, yes.
Or actually as you just want to see whether it compiles compileKotlin would also be enough.

How can I set property it?

Check your IDE project settings.

Does toolchain doesn’t help it?

Probably depends on your IDE and its version.
Latest IJ versions should usually support I think.
But there could also be bugs in that support as it is relatively new.