Compiling with alternative jdk on bazel 6.x

658 views
Skip to first unread message

David Bell

unread,
Nov 28, 2021, 9:03:21 PM11/28/21
to bazel-discuss
hey all,

I'm trying to configure bazel to use a specific jdk for compilation. Going off https://docs.bazel.build/versions/main/bazel-and-java.html I feel like I've defined things sufficiently such that bazel will pick up the right JDK, however the build fails with the following

```
ERROR: Misconfigured toolchains: //buildconfig/java:default_toolchain is declared as a toolchain but has inappropriate dependencies. Declared toolchains should be created with the 'toolchain' rule and should not have dependencies that themselves require toolchains.
.-> //buildconfig/java:default_toolchain
|   @bazel_tools//tools/jdk:platformclasspath
|   @bazel_tools//tools/jdk:current_java_runtime
|   toolchain types @bazel_tools//tools/jdk:runtime_toolchain_type
|   toolchain type @bazel_tools//tools/jdk:runtime_toolchain_type
|   RegisteredToolchains
`-- //buildconfig/java:default_toolchain
```

When I remove the default toolchain compilation succeeds, however the toolchain picked up for compilation is the remotejdk_17 toolchain defined by bazel. Anyone know what's up, or have an example I can work off of?

WORKSPACE
```
load("//buildconfig/java:jdks.bzl", "jdk_repositories")
jdk_repositories()
register_toolchains("//buildconfig/java:default_toolchain")
```

//buildconfig/java/BUILD
```
load("@bazel_tools//tools/jdk:default_java_toolchain.bzl", "default_java_toolchain", "DEFAULT_TOOLCHAIN_CONFIGURATION", "JDK9_JVM_OPTS", "DEFAULT_JAVACOPTS")

default_java_toolchain(
  name = "default_toolchain",
  configuration = DEFAULT_TOOLCHAIN_CONFIGURATION,
  java_runtime = "@corretto_jdk17_macos_aarch64//:jdk",
  source_version = "17",
  target_version = "17",
)
```

//buildconfig/java/jdks.bzl
```
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
load("@bazel_tools//tools/jdk:remote_java_repository.bzl", "remote_java_repository")

def jdk_repositories():
    maybe(
        remote_java_repository,
        name = "corretto_jdk17_macos_aarch64",
        prefix = "corretto",
        version = "17",
        exec_compatible_with = [
            "@platforms//os:macos",
            "@platforms//cpu:aarch64",
        ],
        sha256 = "56a00b4b6d76cd1cc80f515a06fe1b36e3dfff2083d6fb97f5375ae5fe9dac2b",
        strip_prefix = "amazon-corretto-17.jdk/Contents/Home",
        urls = [
        ],
    )
```

.bazelrc
```
build --java_language_version=17
build --java_runtime_version=corretto_17
build --tool_java_runtime_version=corretto_17
test --java_language_version=17
test --java_runtime_version=corretto_17
test --tool_java_runtime_version=corretto_17
```

.bazelversion
```
6.0.0-pre.20211110.1
```

Ted Kaplan

unread,
Feb 23, 2024, 6:00:23 PMFeb 23
to bazel-discuss
The toolchain that is created by the default_java_toolchain macro adds the suffix "_definition", so the correct call to register_toolchains would look like:

register_toolchains("//buildconfig/java:default_toolchain_definition")

This is mentioned in the docs, but is easy to overlook.
Reply all
Reply to author
Forward
0 new messages