bazelbuild / rules_java

Java rules for Bazel
Apache License 2.0
75 stars 66 forks source link

Upgrade Java runtime to 21 while keeping the compiler toolchain as 17 #244

Open yzhangyext opened 1 week ago

yzhangyext commented 1 week ago
Version:
Bazel: 7.2.1
rules_java: 7.1.0

Hi team, we are planning to upgrade our bazel Java version to 21 in two steps, upgrading runtime first, followed by the toolchain compiler upgrade. I am now stuck at making the configuration work for Java 21 runtime while keeping the toolchain version as 17, Our current setting with Java 17 in Bazel is, set these flags in .bazelrc

build --java_runtime_version=remotejdk_17
build --tool_java_runtime_version=remotejdk_17
build --java_language_version=17
build --tool_java_language_version=17

Then register a toolchain with "default_java_toolchain"

default_java_toolchain(
    name = "toolchain_jdk_17",
    java_runtime = "@bazel_tools//tools/jdk:remotejdk_17",
    source_version = "17",
    target_version = "17",
    # other params
)

My first attempt was to bump these two flags as

build --java_runtime_version=remotejdk_21
build --tool_java_runtime_version=remotejdk_21

However I was seeing some build errors like

error: [BazelJavaConfiguration] The Java 17 runtime used to run javac is not recent enough to compile for the Java 21 runtime in external/remotejdk21_macos_aarch64. Either register a Java toolchain with a newer java_runtime or specify a lower --tool_java_runtime_version.
...
error: cannot access module-info
  bad class file: /modules/java.security.sasl/module-info.class
    class file has wrong version 65.0, should be 61.0
    Please remove or make sure it appears in the correct subdirectory of the classpath.

Then I go ahead and also change the java_runtime field within default_java_toolchain:

default_java_toolchain(
    name = "toolchain_jdk_17",
    java_runtime = "@rules_java//toolchains:remotejdk_21",
    source_version = "17",
    target_version = "17",
    # other params
)

That makes the build pass, however I noticed the java compiler was actually 21 as well, while I expected it to stay 17, because I added some code related to VirtualThread and they were compiled successfully.

Did I make any mistakes in my configurations? Appreciate any pointers and tips.

hvadehra commented 3 days ago

This looks like https://github.com/bazelbuild/bazel/discussions/21769 and will possibly be fixed by https://github.com/bazelbuild/rules_java/pull/182. Could you try building by overriding rules_java to the state of that PR?