Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
yzhangyext opened this issue Nov 25, 2024 · 3 comments
Open
Labels
P2 We'll consider to work on this in future. (Assignee optional)

Comments

@yzhangyext
Copy link

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
Copy link
Member

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

@hvadehra hvadehra added the P2 We'll consider to work on this in future. (Assignee optional) label Nov 29, 2024
@yzhangyext
Copy link
Author

Hi, thanks for the quick fix! However I am having some trouble applying this change: upgrading to rules_java v8 will break our existing protobuf settings. I tried cherry-picking that change on top of version 7.12.4, but building with the --@rules_java//java:incompatible_language_version_bootclasspath flag didn't change the bootclasspath behavior. Do you know if that change could be backported to version 7?

@hvadehra
Copy link
Member

hvadehra commented Jan 9, 2025

However I am having some trouble applying this change: upgrading to rules_java v8 will break our existing protobuf settings

  • What is your Bazel version?
  • Are you using bzlmod (x)or WORKSPACE?
  • What version of protobuf are you currently on?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P2 We'll consider to work on this in future. (Assignee optional)
Projects
None yet
Development

No branches or pull requests

2 participants