Replies: 3 comments 2 replies
-
compiling for Java N, it is important to use the boot classes from JDK N to prevent Java N+ types from leaking into the code, which can lead to compilation success but runtime failures. |
Beta Was this translation helpful? Give feedback.
-
I prototyped this as an incompatible flag for rules_java over at bazelbuild/rules_java#182. Instead of matching on Please let me know what you think. |
Beta Was this translation helpful? Give feedback.
-
When compiling for Java N, one must use the same boot classes from JDK N. Failing to do that will allow Java N+ types to leak into the code such that it will compile but later fail at runtime. Using the example from Baeldung's A Guide to Java Source and Target Options (snippet reproduced below), this will successfully compile with
--java_language_version=8
even thoughList.of
didn't appear until Java 9. Only by specifying--java_runtime_version=8
will I encounter the (desired)cannot find symbol
error.Bazel's Java rules are sensitive to three different toolchain types (reference):
@bazel_tools//tools/jdk:toolchain_type
@bazel_tools//tools/jdk:runtime_toolchain_type
@bazel_tools//tools/jdk:bootstrap_runtime_toolchain_type
A toolchain of
bootstrap_runtime_toolchain_type
is an input to thebootclasspath
rule. Boot classes are extracted from that toolchain. Today, the toolchain is selected based on a match of thejava_runtime_version
CLI option (ref, seetarget_settings
).Should
bootstrap_runtime_toolchain_type
bind tojava_language_version
instead? Doing so would constrain boot/platform classes to a project's Java language version and guard against the situation described above.Beta Was this translation helpful? Give feedback.
All reactions