Skip to content

platformclasspath is incorrectly based on --tool_java_runtime_version #19537

@fmeum

Description

@fmeum

Description of the bug:

The contents of the platformclasspath.jar seem to depend on the Java runtime specified via --tool_java_runtime_version, not the one specified via --java_runtime_version (as long as --java_runtime_version specifies a version >= 9). This is incorrect as the latter controls the runtime environment for regular Java targets built in the Java configuration.

As a result, Java cross-compilation for the specified Java runtime can succeed even though the target will fail at runtime.

Which category does this issue belong to?

Team-Java

What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

Invalid platformclasspath.jar contents:

  1. In any Bazel project, run USE_BAZEL_VERSION=500967715d64874b384e9a4c43d96ed6531c0f77 bazel build @rules_java//toolchains:platformclasspath --java_runtime_version=remotejdk_20.
  2. Observe that the generated jar does not contain java/util/HexFormat.class, which was added in Java 17.
  3. Run USE_BAZEL_VERSION=500967715d64874b384e9a4c43d96ed6531c0f77 bazel build @rules_java//toolchains:platformclasspath --java_runtime_version=remotejdk_20 --tool_java_runtime_version=remotejdk_20.
  4. Observe that the generated jar does contain java/util/HexFormat.class.

Cross-compilation only failing at runtime:

# BUILD.bazel
java_binary(
    name = "Main",
    srcs = ["Main.java"],
    main_class = "com.example.Main",
)
# Main.java
package com.example;

import java.util.HexFormat;
import java.util.Optional;
import java.util.Random;

public class Main {
  public static void main(String[] args) {
    # Java 11
    System.out.println(Optional.ofNullable("Hello World!").orElseThrow());
    # Java 17
    System.out.println(HexFormat.of().formatHex(new byte[] { 0x01, 0x02, 0x03 }));
  }
}

$ USE_BAZEL_VERSION=500967715d64874b384e9a4c43d96ed6531c0f77 bazel run :Main --java_language_version=8 --java_runtime_version=remotejdk_11 --tool_java_runtime_version=remotejdk_20
INFO: Invocation ID: 60d37c92-9efc-4562-bfa1-857a95462b7f
INFO: Analyzed target //:Main (49 packages loaded, 820 targets configured).
INFO: Found 1 target...
Target //:Main up-to-date:
  bazel-bin/Main
  bazel-bin/Main.jar
INFO: Elapsed time: 12.500s, Critical Path: 2.82s
INFO: 3 processes: 2 internal, 1 worker.
INFO: Build completed successfully, 3 total actions
INFO: Running command line: bazel-bin/Main
Hello World!
Exception in thread "main" java.lang.NoClassDefFoundError: java/util/HexFormat
	at com.example.Main.main(Main.java:10)
Caused by: java.lang.ClassNotFoundException: java.util.HexFormat
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
	... 1 more

Which operating system are you running Bazel on?

Linux

What is the output of bazel info release?

dev (last_green)

If bazel info release returns development version or (@non-git), tell us how you built Bazel.

No response

What's the output of git remote get-url origin; git rev-parse master; git rev-parse HEAD ?

No response

Is this a regression? If yes, please try to identify the Bazel commit where the bug was introduced.

No response

Have you found anything relevant by searching the web?

No response

Any other information, logs, or outputs that you want to share?

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions