Improve errors in integration tests  when the local jdk is not found

Related:

https://github.com/bazelbuild/bazel/issues/21391 / https://github.com/bazelbuild/bazel/pull/21392

https://github.com/bazelbuild/bazel/pull/22228

Closes #22285.

PiperOrigin-RevId: 633466127
Change-Id: I6334c655607807073da2eda88b1fe66a81fb8ba7
diff --git a/src/test/shell/testenv.sh.tmpl b/src/test/shell/testenv.sh.tmpl
index 8a57f2e..e4f3c15 100755
--- a/src/test/shell/testenv.sh.tmpl
+++ b/src/test/shell/testenv.sh.tmpl
@@ -258,9 +258,19 @@
 
 function setup_localjdk_javabase() {
   if [[ $PLATFORM =~ msys ]]; then
-    jdk_dir="$(cygpath -m $(cd $(rlocation local_jdk/bin/java.exe)/../..; pwd))"
+    jdk_binary=local_jdk/bin/java.exe
   else
-    jdk_dir="$(dirname $(dirname $(rlocation local_jdk/bin/java)))"
+    jdk_binary=local_jdk/bin/java
+  fi
+  jdk_binary_rlocation=$(rlocation ${jdk_binary})
+  if [[ -z "${jdk_binary_rlocation}" ]]; then
+    echo "error: failed to find $jdk_binary, make sure you have java \
+installed or pass --java_runtime_verison=XX with the correct version" >&2
+  fi
+  if [[ $PLATFORM =~ msys ]]; then
+    jdk_dir="$(cygpath -m $(cd ${jdk_binary_rlocation}/../..; pwd))"
+  else
+    jdk_dir="$(dirname $(dirname ${jdk_binary_rlocation}))"
   fi
   bazel_javabase="${jdk_dir}"
 }