Fix #7304: Validate that JAVA_HOME points to a JDK.

If JAVA_HOME is not valid, fallback to the detection by looking up the
`javac` binary.

This also fixes an issue where Bazel would try to look up the boot classpath
for JDK 8 in the wrong folder: By convention, OpenJDK 8 has a directory
structure, where the JRE is in a subdirectory of the JDK:

/usr/lib/jvm/java-8-openjdk-amd64 <- JDK
/usr/lib/jvm/java-8-openjdk-amd64/jre <- JRE

This changed with OpenJDK 9 and newer and there is no longer a `jre`
folder for these versions (and newer Java versions are not supplied as a
separate "JRE" version anymore, there is only a "JDK").

On some platforms (e.g. Debian) the JAVA_HOME of an installed OpenJDK 8
might point to the JRE instead of the JDK, which triggers a bug in the
boot classpath detection logic in Bazel's DumpPlatformClassPath tool:
The tool relies on the directory structure as mentioned above to
determine whether it is looking at a OpenJDK 8 or OpenJDK >= 9. If
certain well-known files (e.g. "jre/lib/rt.jar") of OpenJDK 8 don't
exist, it assumes that it's looking at an OpenJDK 9+ - unfortunately
this is also the case when you're looking at something that's not a JDK
at all or the `jre` subfolder of an OpenJDK 8.

This triggers an InvalidArgumentException in the Javac code.

By making sure that we only ever pass a valid JDK path to that tool, the
problem can no longer happen.

RELNOTES: Bazel now validates that JAVA_HOME points to a valid JDK and
falls back to auto-detection by looking up the path of `javac`.

Closes #7324.

PiperOrigin-RevId: 232197212
4 files changed
tree: f48f1a62b7e72873b1403718cfa10846efc9fbe3
  1. .bazelci/
  2. examples/
  3. scripts/
  4. site/
  5. src/
  6. third_party/
  7. tools/
  8. .gitattributes
  9. .gitignore
  10. AUTHORS
  11. BUILD
  12. CHANGELOG.md
  13. CODEOWNERS
  14. combine_distfiles.py
  15. combine_distfiles_to_tar.sh
  16. compile.sh
  17. CONTRIBUTING.md
  18. CONTRIBUTORS
  19. distdir.bzl
  20. ISSUE_TEMPLATE.md
  21. LICENSE
  22. README.md
  23. WORKSPACE
README.md

Bazel

{Fast, Correct} - Choose two

Build and test software of any size, quickly and reliably.

  • Speed up your builds and tests: Bazel only rebuilds what is necessary. With advanced local and distributed caching, optimized dependency analysis and parallel execution, you get fast and incremental builds.

  • One tool, multiple languages: Build and test Java, C++, Android, iOS, Go, and a wide variety of other language platforms. Bazel runs on Windows, macOS, and Linux.

  • Scalable: Bazel helps you scale your organization, codebase, and continuous integration solution. It handles codebases of any size, in multiple repositories or a huge monorepo.

  • Extensible to your needs: Easily add support for new languages and platforms with Bazel's familiar extension language. Share and re-use language rules written by the growing Bazel community.

Getting Started

Documentation

Contributing to Bazel

See CONTRIBUTING.md

Build status

Bazel is released in ‘Beta’. See the product roadmap to learn about the path toward a stable 1.0 release.