Acquire an instance of JavaToolchainService for a (given) path?

Is there any way to get an instance of JavaToolchainService for a particular file system path? I like all the toolchain automatic-detections but for low-level JDK developers providing a path to a JDK and the JDK version is an overkill. What I’m looking for is a way to instantiate JavaToolchainService for the provided filesystem path (regardless of the JDK version present there), similar to what used to be possible via:


If there is a way to do it via public API – all the better. But even internal APIs would be a good workaround.

I think what you are after is Possibility to manually create a Toolchain programmatically · Issue #16245 · gradle/gradle · GitHub

Yes, exactly that. I dug in gradle’s code internally and I see that it actually does have a “spec” that is handled exceptionally - SpecificInstallationToolchainSpec… but this seems like a hackish way to do it and I’d much more prefer a public API to just get the toolchain for a particular InstallationLocation. Everything seems to be possible via the public API except getting the toolchain for a particular location (not an auto-discovered one).

Auto-discovery and filtering don’t work for folks like me who have multiple versions of literally identical JVM (built from sources, with different patches applied). I currently used internal Gradle classes bypassing toolchains entirely due to these obstacles [1][2].

[2] LUCENE-9990: gradle7 support by dweiss · Pull Request #175 · apache/lucene · GitHub